Skip to content

Commit

Permalink
Add specific logs
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed May 27, 2024
1 parent d4dd6cb commit f7f9863
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ public enum LogLevel {
ERROR
}

public enum SpecificLog {
NETWORK("network"),
MODELER("modeler"),
PARAMETERS("param"),
VARIABLES("variables"),
EQUATIONS("equations");

private final String fileName;

SpecificLog(String fileName) {
this.fileName = fileName;
}

public String getFileName() {
return fileName;
}
}

private Map<String, ParametersSet> modelsParameters = new LinkedHashMap<>();
private ParametersSet networkParameters;
private ParametersSet solverParameters;
Expand All @@ -91,6 +109,7 @@ public enum LogLevel {
private double precision = DEFAULT_PRECISION;
private ExportMode timelineExportMode = DEFAULT_TIMELINE_EXPORT_MODE;
private LogLevel logLevelFilter = DEFAULT_LOG_LEVEL_FILTER;
private EnumSet<SpecificLog> specificLogs = EnumSet.noneOf(SpecificLog.class);

/**
* Loads parameters from the default platform configuration.
Expand Down Expand Up @@ -132,6 +151,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem
c.getOptionalDoubleProperty("precision").ifPresent(parameters::setPrecision);
c.getOptionalEnumProperty("timeline.exportMode", ExportMode.class).ifPresent(parameters::setTimelineExportMode);
c.getOptionalEnumProperty("log.levelFilter", LogLevel.class).ifPresent(parameters::setLogLevelFilter);
c.getOptionalEnumSetProperty("log.specificLogs", SpecificLog.class).ifPresent(parameters::setSpecificLogs);
});
return parameters;
}
Expand Down Expand Up @@ -263,4 +283,18 @@ public DynaWaltzParameters setLogLevelFilter(LogLevel logLevelFilter) {
this.logLevelFilter = logLevelFilter;
return this;
}

public Set<SpecificLog> getSpecificLogs() {
return specificLogs;
}

public DynaWaltzParameters setSpecificLogs(Set<SpecificLog> specificLogs) {
this.specificLogs = EnumSet.copyOf(specificLogs);
return this;
}

public DynaWaltzParameters addSpecificLog(SpecificLog specificLog) {
specificLogs.add(specificLog);
return this;
}
}
14 changes: 12 additions & 2 deletions dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/JobsXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static void writeOutput(XMLStreamWriter writer, DynaWaltzContext context
writer.writeAttribute("global", Boolean.toString(false));

writer.writeEmptyElement(DYN_URI, "timeline");
writer.writeAttribute("exportMode", context.getDynaWaltzParameters().getTimelineExportMode().name());
writer.writeAttribute("exportMode", parameters.getTimelineExportMode().toString());

writer.writeEmptyElement(DYN_URI, "finalState");
writer.writeAttribute("exportIIDMFile", Boolean.toString(parameters.isWriteFinalState()));
Expand All @@ -105,7 +105,7 @@ private static void writeOutput(XMLStreamWriter writer, DynaWaltzContext context
if (context.withCurves()) {
writer.writeEmptyElement(DYN_URI, "curves");
writer.writeAttribute("inputFile", DynaWaltzConstants.CRV_FILENAME);
writer.writeAttribute("exportMode", DynaWaltzParameters.ExportMode.CSV.name());
writer.writeAttribute("exportMode", DynaWaltzParameters.ExportMode.CSV.toString());
}

writer.writeStartElement(DYN_URI, "logs");
Expand All @@ -120,5 +120,15 @@ private static void writeAppender(XMLStreamWriter writer, DynaWaltzParameters pa
writer.writeAttribute("tag", "");
writer.writeAttribute("file", "dynawaltz.log");
writer.writeAttribute("lvlFilter", parameters.getLogLevelFilter().toString());
for (DynaWaltzParameters.SpecificLog log : parameters.getSpecificLogs()) {
writeSpecificAppender(writer, log);
}
}

private static void writeSpecificAppender(XMLStreamWriter writer, DynaWaltzParameters.SpecificLog log) throws XMLStreamException {
writer.writeEmptyElement(DYN_URI, "appender");
writer.writeAttribute("tag", log.toString());
writer.writeAttribute("file", log.getFileName() + ".log");
writer.writeAttribute("lvlFilter", DynaWaltzParameters.LogLevel.DEBUG.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.dynawaltz.DynaWaltzParameters.ExportMode;
import com.powsybl.dynawaltz.DynaWaltzParameters.LogLevel;
import com.powsybl.dynawaltz.DynaWaltzParameters.SolverType;
import com.powsybl.dynawaltz.DynaWaltzParameters.SpecificLog;
import com.powsybl.dynawaltz.parameters.Parameter;
import com.powsybl.dynawaltz.parameters.ParameterType;
import com.powsybl.dynawaltz.xml.ParametersXml;
Expand All @@ -25,8 +26,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -63,7 +63,8 @@ void checkParameters() throws IOException {
double precision = 1e-8;
ExportMode timelinExportMode = ExportMode.XML;
LogLevel logLevel = LogLevel.WARN;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers, precision, timelinExportMode, logLevel);
Set<SpecificLog> specificLogs = EnumSet.of(SpecificLog.MODELER, SpecificLog.EQUATIONS);
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers, precision, timelinExportMode, logLevel, specificLogs);

DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem);

Expand Down Expand Up @@ -97,6 +98,7 @@ void checkParameters() throws IOException {
assertEquals(precision, parameters.getPrecision());
assertEquals(timelinExportMode, parameters.getTimelineExportMode());
assertEquals(logLevel, parameters.getLogLevelFilter());
assertEquals(specificLogs, parameters.getSpecificLogs());
}

@Test
Expand All @@ -119,7 +121,7 @@ void roundTripParametersSerializing() throws IOException {
SolverType solverType = SolverType.IDA;
String solverParametersId = "solverParametersId";
boolean mergeLoads = false;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false, 1e-7, ExportMode.TXT, LogLevel.INFO);
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false, 1e-7, ExportMode.TXT, LogLevel.INFO, Set.of(SpecificLog.PARAMETERS, SpecificLog.VARIABLES));

DynamicSimulationParameters dynamicSimulationParameters = new DynamicSimulationParameters()
.setStartTime(0)
Expand All @@ -130,7 +132,9 @@ void roundTripParametersSerializing() throws IOException {
JsonDynamicSimulationParameters::read, "/DynaWaltzParameters.json");
}

private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelSimplifiers, double precision, ExportMode timelineExportMode, DynaWaltzParameters.LogLevel logLevel) throws IOException {
private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId,
boolean mergeLoads, boolean useModelSimplifiers, double precision, ExportMode timelineExportMode,
LogLevel logLevel, Set<SpecificLog> specificLogs) throws IOException {
String parametersFile = USER_HOME + "parametersFile";
String networkParametersFile = USER_HOME + "networkParametersFile";
String solverParametersFile = USER_HOME + "solverParametersFile";
Expand All @@ -147,6 +151,8 @@ private void initPlatformConfig(String networkParametersId, SolverType solverTyp
moduleConfig.setStringProperty("precision", Double.toString(precision));
moduleConfig.setStringProperty("timeline.exportMode", String.valueOf(timelineExportMode));
moduleConfig.setStringProperty("log.levelFilter", logLevel.toString());
moduleConfig.setStringListProperty("log.specificLogs", specificLogs.stream().map(SpecificLog::toString).toList());


Files.createDirectories(fileSystem.getPath(USER_HOME));
copyFile("/parametersSet/models.par", parametersFile);
Expand Down
12 changes: 12 additions & 0 deletions dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/JobsXmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.EnumSet;

/**
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
Expand Down Expand Up @@ -42,4 +43,15 @@ void writeJobWithDumpFile() throws SAXException, IOException {
validate("jobs.xsd", "jobsWithDump.xml", tmpDir.resolve(DynaWaltzConstants.JOBS_FILENAME));
}

@Test
void writeJobWithSpecificLogs() throws SAXException, IOException {
DynamicSimulationParameters parameters = DynamicSimulationParameters.load();
DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load()
.setSpecificLogs(EnumSet.allOf(DynaWaltzParameters.SpecificLog.class));
DynaWaltzContext context = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, parameters, dynawoParameters);

JobsXml.write(tmpDir, context);
validate("jobs.xsd", "jobsWithSpecificLogs.xml", tmpDir.resolve(DynaWaltzConstants.JOBS_FILENAME));
}

}
1 change: 1 addition & 0 deletions dynawaltz/src/test/resources/DynaWaltzParameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"precision" : 1.0E-7,
"timelineExportMode" : "TXT",
"logLevelFilter" : "INFO",
"specificLogs" : [ "PARAMETERS", "VARIABLES" ],
"modelsParameters" : [ {
"id" : "test",
"parameters" : {
Expand Down
27 changes: 27 additions & 0 deletions dynawaltz/src/test/resources/jobsWithSpecificLogs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<dyn:jobs xmlns:dyn="http://www.rte-france.com/dynawo">
<dyn:job name="Job">
<dyn:solver lib="dynawo_SolverIDA" parFile="solvers.par" parId="1"/>
<dyn:modeler compileDir="outputs/compilation">
<dyn:network iidmFile="powsybl_dynawaltz.xiidm" parFile="network.par" parId="1"/>
<dyn:dynModels dydFile="powsybl_dynawaltz.dyd"/>
<dyn:precompiledModels useStandardModels="true"/>
<dyn:modelicaModels useStandardModels="false"/>
</dyn:modeler>
<dyn:simulation startTime="1" stopTime="100" precision="1.0E-6"/>
<dyn:outputs directory="outputs">
<dyn:dumpInitValues local="false" global="false"/>
<dyn:timeline exportMode="TXT"/>
<dyn:finalState exportIIDMFile="true" exportDumpFile="false"/>
<dyn:curves inputFile="powsybl_dynawaltz.crv" exportMode="CSV"/>
<dyn:logs>
<dyn:appender tag="" file="dynawaltz.log" lvlFilter="INFO"/>
<dyn:appender tag="NETWORK" file="network.log" lvlFilter="DEBUG"/>
<dyn:appender tag="MODELER" file="modeler.log" lvlFilter="DEBUG"/>
<dyn:appender tag="PARAMETERS" file="param.log" lvlFilter="DEBUG"/>
<dyn:appender tag="VARIABLES" file="variables.log" lvlFilter="DEBUG"/>
<dyn:appender tag="EQUATIONS" file="equations.log" lvlFilter="DEBUG"/>
</dyn:logs>
</dyn:outputs>
</dyn:job>
</dyn:jobs>

0 comments on commit f7f9863

Please sign in to comment.