Skip to content

Commit

Permalink
Merge branch 'main' into bump_6_4_snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisrte authored May 30, 2024
2 parents eb43efd + c0fed4c commit 841c99b
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
package com.powsybl.dynawaltz;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.config.ModuleConfig;

import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

Expand All @@ -22,6 +26,7 @@ public record DumpFileParameters(boolean exportDumpFile, boolean useDumpFile, Pa
public static final boolean DEFAULT_USE_DUMP = false;
public static final String DEFAULT_DUMP_FOLDER = null;
public static final String DEFAULT_DUMP_NAME = null;
public static final DumpFileParameters DEFAULT_DUMP_FILE_PARAMETERS = new DumpFileParameters(DEFAULT_EXPORT_DUMP, DEFAULT_USE_DUMP, null, DEFAULT_DUMP_NAME);

public DumpFileParameters {
if (useDumpFile) {
Expand All @@ -33,7 +38,7 @@ public record DumpFileParameters(boolean exportDumpFile, boolean useDumpFile, Pa
}

public static DumpFileParameters createDefaultDumpFileParameters() {
return new DumpFileParameters(DEFAULT_EXPORT_DUMP, DEFAULT_USE_DUMP, null, DEFAULT_DUMP_NAME);
return DEFAULT_DUMP_FILE_PARAMETERS;
}

public static DumpFileParameters createExportDumpFileParameters(Path dumpFileFolder) {
Expand All @@ -48,6 +53,22 @@ public static DumpFileParameters createImportExportDumpFileParameters(Path dumpF
return new DumpFileParameters(true, true, dumpFileFolder, dumpFile);
}

public static DumpFileParameters createDumpFileParametersFromConfig(ModuleConfig config, FileSystem fileSystem) {
boolean exportDumpFile = config.getOptionalBooleanProperty("dump.export").orElse(DumpFileParameters.DEFAULT_EXPORT_DUMP);
String exportDumpFileFolder = config.getOptionalStringProperty("dump.exportFolder").orElse(DumpFileParameters.DEFAULT_DUMP_FOLDER);
Path exportDumpFileFolderPath = exportDumpFileFolder != null ? fileSystem.getPath(exportDumpFileFolder) : null;
boolean exportFolderNotFound = exportDumpFileFolderPath == null || !Files.exists(exportDumpFileFolderPath);
if (exportDumpFile && exportFolderNotFound) {
throw new PowsyblException("Folder " + exportDumpFileFolder + " set in 'dumpFileFolder' property cannot be found");
}
boolean useDumpFile = config.getOptionalBooleanProperty("dump.useAsInput").orElse(DumpFileParameters.DEFAULT_USE_DUMP);
String dumpFile = config.getOptionalStringProperty("dump.fileName").orElse(DumpFileParameters.DEFAULT_DUMP_NAME);
if (useDumpFile && (exportFolderNotFound || dumpFile == null || !Files.exists(exportDumpFileFolderPath.resolve(dumpFile)))) {
throw new PowsyblException("File " + dumpFile + " set in 'dumpFile' property cannot be found");
}
return new DumpFileParameters(exportDumpFile, useDumpFile, exportDumpFileFolderPath, dumpFile);
}

public Path getDumpFilePath() {
return dumpFileFolder != null ?
dumpFileFolder.resolve(dumpFile)
Expand Down
142 changes: 76 additions & 66 deletions dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

Expand All @@ -31,29 +30,58 @@ public class DynaWaltzParameters extends AbstractExtension<DynamicSimulationPara
public static final SolverType DEFAULT_SOLVER_TYPE = SolverType.SIM;
public static final String DEFAULT_NETWORK_PAR_ID = "1";
public static final String DEFAULT_SOLVER_PAR_ID = "1";
public static final boolean DEFAULT_MERGE_LOADS = true;
public static final boolean DEFAULT_MERGE_LOADS = false;
public static final String DEFAULT_INPUT_PARAMETERS_FILE = "models.par";
public static final String DEFAULT_INPUT_NETWORK_PARAMETERS_FILE = "network.par";
public static final String DEFAULT_INPUT_SOLVER_PARAMETERS_FILE = "solvers.par";
public static final String MODELS_OUTPUT_PARAMETERS_FILE = "models.par";
public static final String NETWORK_OUTPUT_PARAMETERS_FILE = "network.par";
public static final String SOLVER_OUTPUT_PARAMETERS_FILE = "solvers.par";
private static final boolean DEFAULT_WRITE_FINAL_STATE = true;
public static final boolean USE_MODEL_SIMPLIFIERS = false;
public static final boolean DEFAULT_USE_MODEL_SIMPLIFIERS = false;
public static final double DEFAULT_PRECISION = 1e-6;
public static final ExportMode DEFAULT_TIMELINE_EXPORT_MODE = ExportMode.TXT;

/**
* Information about the solver to use in the simulation
*/
public enum SolverType {
/**
* the simplified solver
*/
SIM,
/**
* the IDA solver
*/
IDA
}

public enum ExportMode {
CSV(".csv"),
TXT(".log"),
XML(".xml");

private final String fileExtension;

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

public String getFileExtension() {
return fileExtension;
}
}

private Map<String, ParametersSet> modelsParameters = new LinkedHashMap<>();
private ParametersSet networkParameters;
private ParametersSet solverParameters;
private SolverType solverType;
private boolean mergeLoads;
private SolverType solverType = DEFAULT_SOLVER_TYPE;
private boolean mergeLoads = DEFAULT_MERGE_LOADS;
private boolean writeFinalState = DEFAULT_WRITE_FINAL_STATE;
private boolean useModelSimplifiers = USE_MODEL_SIMPLIFIERS;
private DumpFileParameters dumpFileParameters;
private boolean useModelSimplifiers = DEFAULT_USE_MODEL_SIMPLIFIERS;
private DumpFileParameters dumpFileParameters = DumpFileParameters.DEFAULT_DUMP_FILE_PARAMETERS;
private double precision = DEFAULT_PRECISION;
private ExportMode timelineExportMode = DEFAULT_TIMELINE_EXPORT_MODE;

/**
* Loads parameters from the default platform configuration.
Expand All @@ -70,72 +98,36 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig) {
}

public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem fileSystem) {
DynaWaltzParameters parameters = new DynaWaltzParameters();
Optional<ModuleConfig> config = platformConfig.getOptionalModuleConfig("dynawaltz-default-parameters");

// File with all the dynamic models' parameters for the simulation
String parametersFile = config.map(c -> c.getStringProperty("parametersFile")).orElse(DEFAULT_INPUT_PARAMETERS_FILE);
Path parametersPath = platformConfig.getConfigDir().map(configDir -> configDir.resolve(parametersFile))
.orElse(fileSystem.getPath(parametersFile));

// File with all the network's parameters for the simulation
String networkParametersFile = config.map(c -> c.getStringProperty("network.parametersFile")).orElse(DEFAULT_INPUT_NETWORK_PARAMETERS_FILE);
Path networkParametersPath = platformConfig.getConfigDir().map(configDir -> configDir.resolve(networkParametersFile))
.orElse(fileSystem.getPath(networkParametersFile));

// Identifies the set of network parameters that will be used in the simulation.
String parametersFile = config.flatMap(c -> c.getOptionalStringProperty("parametersFile")).orElse(DEFAULT_INPUT_PARAMETERS_FILE);
String networkParametersFile = config.flatMap(c -> c.getOptionalStringProperty("network.parametersFile")).orElse(DEFAULT_INPUT_NETWORK_PARAMETERS_FILE);
String networkParametersId = config.flatMap(c -> c.getOptionalStringProperty("network.parametersId")).orElse(DEFAULT_NETWORK_PAR_ID);

// Information about the solver to use in the simulation, there are two options
// the simplified solver
// and the IDA solver
SolverType solverType = config.flatMap(c -> c.getOptionalEnumProperty("solver.type", SolverType.class)).orElse(DEFAULT_SOLVER_TYPE);

// File with all the solvers' parameters for the simulation
String solverParametersFile = config.flatMap(c -> c.getOptionalStringProperty("solver.parametersFile")).orElse(DEFAULT_INPUT_SOLVER_PARAMETERS_FILE);
Path solverParametersPath = platformConfig.getConfigDir().map(configDir -> configDir.resolve(solverParametersFile))
.orElse(fileSystem.getPath(solverParametersFile));

// Identifies the set of solver parameters that will be used in the simulation
String solverParametersId = config.flatMap(c -> c.getOptionalStringProperty("solver.parametersId")).orElse(DEFAULT_SOLVER_PAR_ID);
// File with all the dynamic models' parameters for the simulation
parameters.setModelsParameters(ParametersXml.load(resolveParameterPath(parametersFile, platformConfig, fileSystem)))
// File with all the network's parameters for the simulation
.setNetworkParameters(ParametersXml.load(resolveParameterPath(networkParametersFile, platformConfig, fileSystem), networkParametersId))
// File with all the solvers' parameters for the simulation
.setSolverParameters(ParametersXml.load(resolveParameterPath(solverParametersFile, platformConfig, fileSystem), solverParametersId));

// If merging loads on each bus to simplify dynawo's analysis
boolean mergeLoads = config.flatMap(c -> c.getOptionalBooleanProperty("mergeLoads")).orElse(DEFAULT_MERGE_LOADS);

// Writes final state IIDM
boolean writeFinalState = config.flatMap(c -> c.getOptionalBooleanProperty("writeFinalState")).orElse(DEFAULT_WRITE_FINAL_STATE);

boolean useModelSimplifiers = config.flatMap(c -> c.getOptionalBooleanProperty("useModelSimplifiers")).orElse(USE_MODEL_SIMPLIFIERS);

// Dump file config
boolean exportDumpFile = config.flatMap(c -> c.getOptionalBooleanProperty("dump.export")).orElse(DumpFileParameters.DEFAULT_EXPORT_DUMP);
String exportDumpFileFolder = config.flatMap(c -> c.getOptionalStringProperty("dump.exportFolder")).orElse(DumpFileParameters.DEFAULT_DUMP_FOLDER);
Path exportDumpFileFolderPath = exportDumpFileFolder != null ? fileSystem.getPath(exportDumpFileFolder) : null;
boolean exportFolderNotFound = exportDumpFileFolderPath == null || !Files.exists(exportDumpFileFolderPath);
if (exportDumpFile && exportFolderNotFound) {
throw new PowsyblException("Folder " + exportDumpFileFolder + " set in 'dumpFileFolder' property cannot be found");
}
boolean useDumpFile = config.flatMap(c -> c.getOptionalBooleanProperty("dump.useAsInput")).orElse(DumpFileParameters.DEFAULT_USE_DUMP);
String dumpFile = config.flatMap(c -> c.getOptionalStringProperty("dump.fileName")).orElse(DumpFileParameters.DEFAULT_DUMP_NAME);
if (useDumpFile && (exportFolderNotFound || dumpFile == null || !Files.exists(exportDumpFileFolderPath.resolve(dumpFile)))) {
throw new PowsyblException("File " + dumpFile + " set in 'dumpFile' property cannot be found");
}

DumpFileParameters dumpFileParameters = new DumpFileParameters(exportDumpFile, useDumpFile, exportDumpFileFolderPath, dumpFile);

// Load xml files
List<ParametersSet> modelsParameters = ParametersXml.load(parametersPath);
ParametersSet networkParameters = ParametersXml.load(networkParametersPath, networkParametersId);
ParametersSet solverParameters = ParametersXml.load(solverParametersPath, solverParametersId);
config.ifPresent(c -> {
parameters.setDumpFileParameters(DumpFileParameters.createDumpFileParametersFromConfig(c, fileSystem));
c.getOptionalEnumProperty("solver.type", SolverType.class).ifPresent(parameters::setSolverType);
// If merging loads on each bus to simplify dynawo's analysis
c.getOptionalBooleanProperty("mergeLoads").ifPresent(parameters::setMergeLoads);
c.getOptionalBooleanProperty("writeFinalState").ifPresent(parameters::setWriteFinalState);
c.getOptionalBooleanProperty("useModelSimplifiers").ifPresent(parameters::setUseModelSimplifiers);
c.getOptionalDoubleProperty("precision").ifPresent(parameters::setPrecision);
c.getOptionalEnumProperty("timeline.exportMode", ExportMode.class).ifPresent(parameters::setTimelineExportMode);
});
return parameters;
}

return new DynaWaltzParameters()
.setModelsParameters(modelsParameters)
.setNetworkParameters(networkParameters)
.setSolverParameters(solverParameters)
.setSolverType(solverType)
.setMergeLoads(mergeLoads)
.setWriteFinalState(writeFinalState)
.setUseModelSimplifiers(useModelSimplifiers)
.setDumpFileParameters(dumpFileParameters);
private static Path resolveParameterPath(String fileName, PlatformConfig platformConfig, FileSystem fileSystem) {
return platformConfig.getConfigDir().map(configDir -> configDir.resolve(fileName)).orElse(fileSystem.getPath(fileName));
}

@Override
Expand Down Expand Up @@ -234,4 +226,22 @@ public DynaWaltzParameters setDefaultDumpFileParameters() {
this.dumpFileParameters = DumpFileParameters.createDefaultDumpFileParameters();
return this;
}

public double getPrecision() {
return precision;
}

public DynaWaltzParameters setPrecision(double precision) {
this.precision = precision;
return this;
}

public ExportMode getTimelineExportMode() {
return timelineExportMode;
}

public DynaWaltzParameters setTimelineExportMode(ExportMode timelineExportMode) {
this.timelineExportMode = timelineExportMode;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.powsybl.dynawo.commons.dynawologs.CsvLogParser;
import com.powsybl.dynawo.commons.loadmerge.LoadsMerger;
import com.powsybl.dynawo.commons.timeline.CsvTimeLineParser;
import com.powsybl.dynawo.commons.timeline.TimeLineParser;
import com.powsybl.dynawo.commons.timeline.XmlTimeLineParser;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.serde.NetworkSerDe;
import com.powsybl.timeseries.DoubleTimeSeries;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class DynaWaltzProvider implements DynamicSimulationProvider {
private static final String LOGS_FOLDER = "logs";
private static final String OUTPUT_IIDM_FILENAME = "outputIIDM.xml";
private static final String OUTPUT_DUMP_FILENAME = "outputState.dmp";
private static final String TIMELINE_FILENAME = "timeline.log";
private static final String TIMELINE_FILENAME = "timeline";
private static final String LOGS_FILENAME = "dynawaltz.log";
private static final String ERROR_FILENAME = "dyn_fs_0.err";
private static final String DYNAWO_ERROR_PATTERN = "DYN Error: ";
Expand Down Expand Up @@ -253,10 +255,15 @@ private void setDumpFile(Path outputsFolder, Path dumpFileFolder, Path fileName)
}

private void setTimeline(Path outputsFolder) {
Path timelineFile = outputsFolder.resolve(DYNAWO_TIMELINE_FOLDER).resolve(TIMELINE_FILENAME);
DynaWaltzParameters.ExportMode exportMode = context.getDynaWaltzParameters().getTimelineExportMode();
Path timelineFile = outputsFolder.resolve(DYNAWO_TIMELINE_FOLDER).resolve(TIMELINE_FILENAME + exportMode.getFileExtension());
if (Files.exists(timelineFile)) {
new CsvTimeLineParser().parse(timelineFile).forEach(e ->
timeline.add(new TimelineEvent(e.time(), e.modelName(), e.message())));
TimeLineParser parser = switch (exportMode) {
case CSV -> new CsvTimeLineParser(';');
case TXT -> new CsvTimeLineParser();
case XML -> new XmlTimeLineParser();
};
parser.parse(timelineFile).forEach(e -> timeline.add(new TimelineEvent(e.time(), e.modelName(), e.message())));
} else {
LOGGER.warn("Timeline file not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
*/
package com.powsybl.dynawaltz.models.defaultmodels;

import com.powsybl.dynawaltz.models.InjectionModel;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public abstract class AbstractInjectionDefaultModel extends AbstractDefaultModel {
public abstract class AbstractInjectionDefaultModel extends AbstractDefaultModel implements InjectionModel {

protected AbstractInjectionDefaultModel(String staticId) {
super(staticId);
Expand All @@ -20,6 +22,7 @@ public String getStateValueVarName() {
return "@NAME@_state_value";
}

@Override
public String getSwitchOffSignalEventVarName() {
return getStateValueVarName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.powsybl.dynawaltz.models.defaultmodels;

import com.powsybl.dynawaltz.models.InjectionModel;
import com.powsybl.dynawaltz.models.Model;
import com.powsybl.dynawaltz.models.buses.*;
import com.powsybl.dynawaltz.models.generators.DefaultGenerator;
Expand All @@ -16,11 +17,9 @@
import com.powsybl.dynawaltz.models.lines.DefaultLine;
import com.powsybl.dynawaltz.models.lines.LineModel;
import com.powsybl.dynawaltz.models.loads.DefaultLoad;
import com.powsybl.dynawaltz.models.loads.LoadModel;
import com.powsybl.dynawaltz.models.shunts.DefaultShunt;
import com.powsybl.dynawaltz.models.shunts.ShuntModel;
import com.powsybl.dynawaltz.models.svarcs.DefaultStaticVarCompensator;
import com.powsybl.dynawaltz.models.svarcs.StaticVarCompensatorModel;
import com.powsybl.dynawaltz.models.transformers.DefaultTransformer;
import com.powsybl.dynawaltz.models.transformers.TransformerModel;
import com.powsybl.iidm.network.IdentifiableType;
Expand Down Expand Up @@ -49,13 +48,13 @@ public enum DefaultModelConfiguration {
LineModel.class,
new DefaultModelFactory<>(DefaultLine::new)),
LOAD(IdentifiableType.LOAD,
LoadModel.class,
InjectionModel.class,
new DefaultModelFactory<>(DefaultLoad::new)),
SHUNT_COMPENSATOR(IdentifiableType.SHUNT_COMPENSATOR,
ShuntModel.class,
new DefaultModelFactory<>(DefaultShunt::new)),
STATIC_VAR_COMPENSATOR(IdentifiableType.STATIC_VAR_COMPENSATOR,
StaticVarCompensatorModel.class,
InjectionModel.class,
new DefaultModelFactory<>(DefaultStaticVarCompensator::new)),
TWO_WINDINGS_TRANSFORMER(IdentifiableType.TWO_WINDINGS_TRANSFORMER,
TransformerModel.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.dynawaltz.models.loads;

import com.powsybl.dynawaltz.models.AbstractEquipmentBlackBoxModel;
import com.powsybl.dynawaltz.models.InjectionModel;
import com.powsybl.dynawaltz.models.VarConnection;
import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint;
import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder;
Expand All @@ -18,7 +19,7 @@
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public abstract class AbstractLoad extends AbstractEquipmentBlackBoxModel<Load> implements LoadModel {
public abstract class AbstractLoad extends AbstractEquipmentBlackBoxModel<Load> implements InjectionModel {

protected final String terminalVarName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DefaultLoad extends AbstractInjectionDefaultModel implements LoadModel, ControllableEquipment {
public class DefaultLoad extends AbstractInjectionDefaultModel implements ControllableEquipment {

public DefaultLoad(String staticId) {
super(staticId);
Expand Down
Loading

0 comments on commit 841c99b

Please sign in to comment.