Skip to content

Commit

Permalink
Merge branch 'main' into fix_release_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-dup authored Dec 8, 2023
2 parents af9a846 + 976268f commit 03a55d7
Show file tree
Hide file tree
Showing 129 changed files with 4,852 additions and 705 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ for (Line line : network.lines) {
parameterSetId "CLA"
controlledQuadripole line.id
iMeasurement line.id
iMeasurementSide Branch.Side.TWO
iMeasurementSide TwoSides.TWO
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-iidm-xml-converter</artifactId>
<artifactId>powsybl-iidm-serde</artifactId>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2023, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.commons;

import com.powsybl.commons.PowsyblException;
import com.univocity.parsers.common.ParsingContext;
import com.univocity.parsers.common.ResultIterator;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public abstract class AbstractCsvParser<T> {

protected static final char DEFAULT_SEPARATOR = '|';

private final char separator;

protected AbstractCsvParser(char separator) {
this.separator = separator;
}

public List<T> parse(Path file) {
if (!Files.exists(file)) {
return Collections.emptyList();
}

try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
return parse(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private List<T> parse(BufferedReader reader) {
Objects.requireNonNull(reader);
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setDelimiter(separator);
settings.getFormat().setQuoteEscape('"');
settings.getFormat().setLineSeparator(System.lineSeparator());
settings.setMaxColumns(getNbColumns());
CsvParser csvParser = new CsvParser(settings);
ResultIterator<String[], ParsingContext> iterator = csvParser.iterate(reader).iterator();
return read(iterator);
}

protected List<T> read(ResultIterator<String[], ParsingContext> iterator) {
List<T> logs = new ArrayList<>();
int iLine = 0;
while (iterator.hasNext()) {
iLine++;
String[] tokens = iterator.next();
if (!hasCorrectNbColumns(tokens.length)) {
throw new PowsyblException("Columns of line " + iLine + " are inconsistent");
}
createEntry(tokens).ifPresent(logs::add);
}
return logs;
}

protected abstract Optional<T> createEntry(String[] tokens);

protected abstract boolean hasCorrectNbColumns(int tokensSize);

protected abstract int getNbColumns();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2023, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.commons;

import com.powsybl.commons.reporter.Report;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.TypedValue;
import com.powsybl.dynawo.commons.dynawologs.LogEntry;
import com.powsybl.dynawo.commons.timeline.TimelineEntry;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public final class CommonReports {

private static final String TIME_MS = "timeMsTypedValue";
private static final String ID = "idTypedValue";

private CommonReports() {
}

public static Reporter createDynawoLogReporter(Reporter reporter) {
return reporter.createSubReporter("dynawoLog",
"Dynawo Log");
}

public static void reportTimelineEvent(Reporter reporter, TimelineEntry timelineEntry) {
reporter.report(Report.builder()
.withKey("DynawoTimelineEvent")
.withDefaultMessage("[t=${time}] ${message} on equipment '${identifiableId}'")
.withTypedValue("time", timelineEntry.time(), TIME_MS)
.withTypedValue("identifiableId", timelineEntry.modelName(), ID)
.withValue("message", timelineEntry.message())
.withSeverity(TypedValue.TRACE_SEVERITY)
.build());
}

public static void reportLogEvent(Reporter reporter, LogEntry logEntry) {
reporter.report(Report.builder()
.withKey("DynawoTimelineEvent")
.withDefaultMessage("${message}")
.withValue("message", logEntry.message())
.withSeverity(logEntry.severity())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package com.powsybl.dynawo.commons;

import com.powsybl.iidm.xml.IidmXmlVersion;
import com.powsybl.iidm.serde.IidmVersion;

import java.util.List;

Expand All @@ -22,10 +22,12 @@ private DynawoConstants() {
/**
* write the network to XIIDM v1.4 because currently Dynawo does not support versions above
*/
public static final String IIDM_VERSION = IidmXmlVersion.V_1_4.toString(".");
public static final String IIDM_VERSION = IidmVersion.V_1_4.toString(".");

public static final String DYNAWO_CMD_NAME = "dynawo";

public static final String DYNAWO_TIMELINE_FOLDER = "timeLine";

public static final DynawoVersion VERSION_MIN = new DynawoVersion(1, 5, 0);

public static final List<String> IIDM_EXTENSIONS = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.computation.*;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.xml.XMLExporter;
import com.powsybl.iidm.serde.AbstractTreeDataExporter;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -35,8 +35,8 @@ public static void writeIidm(Network network, Path file) {
Objects.requireNonNull(network);
Objects.requireNonNull(file);
Properties params = new Properties();
params.setProperty(XMLExporter.VERSION, IIDM_VERSION);
params.setProperty(XMLExporter.EXTENSIONS_LIST, String.join(",", IIDM_EXTENSIONS));
params.setProperty(AbstractTreeDataExporter.VERSION, IIDM_VERSION);
params.setProperty(AbstractTreeDataExporter.EXTENSIONS_LIST, String.join(",", IIDM_EXTENSIONS));
network.write("XIIDM", params, file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public static void update(Network targetNetwork, Network sourceNetwork, boolean

private static void updateHvdcLines(Network targetNetwork, Iterable<HvdcLine> hvdcLines) {
for (HvdcLine sourceHvdcLine : hvdcLines) {
Terminal targetTerminal1 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(HvdcLine.Side.ONE).getTerminal();
Terminal targetTerminal2 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(HvdcLine.Side.TWO).getTerminal();
Terminal sourceTerminal1 = sourceHvdcLine.getConverterStation(HvdcLine.Side.ONE).getTerminal();
Terminal sourceTerminal2 = sourceHvdcLine.getConverterStation(HvdcLine.Side.TWO).getTerminal();
Terminal targetTerminal1 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(TwoSides.ONE).getTerminal();
Terminal targetTerminal2 = targetNetwork.getHvdcLine(sourceHvdcLine.getId()).getConverterStation(TwoSides.TWO).getTerminal();
Terminal sourceTerminal1 = sourceHvdcLine.getConverterStation(TwoSides.ONE).getTerminal();
Terminal sourceTerminal2 = sourceHvdcLine.getConverterStation(TwoSides.TWO).getTerminal();
update(targetTerminal1, sourceTerminal1);
update(targetTerminal2, sourceTerminal2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2023, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.commons.dynawologs;

import com.powsybl.dynawo.commons.AbstractCsvParser;

import java.util.Optional;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public final class CsvLogParser extends AbstractCsvParser<LogEntry> {

private static final int NB_COLUMNS = 4;

public CsvLogParser() {
this(DEFAULT_SEPARATOR);
}

public CsvLogParser(char separator) {
super(separator);
}

@Override
protected Optional<LogEntry> createEntry(String[] tokens) {
return tokens.length == NB_COLUMNS ? LogUtils.createLog(tokens[1], tokens[2] + " " + tokens[3])
: LogUtils.createLog(tokens[1], tokens[2]);
}

@Override
protected boolean hasCorrectNbColumns(int tokensSize) {
return tokensSize == NB_COLUMNS - 1 || tokensSize == NB_COLUMNS;
}

@Override
protected int getNbColumns() {
return NB_COLUMNS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2023, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.commons.dynawologs;

import com.powsybl.commons.reporter.TypedValue;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public record LogEntry(TypedValue severity, String message) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2023, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.commons.dynawologs;

import com.powsybl.commons.reporter.TypedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public final class LogUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(LogUtils.class);
private static final String EMPTY_START = "======";
private static final String EMPTY_BREAKER = "------";

private LogUtils() {
}

public static Optional<LogEntry> createLog(String severity, String message) {
if (severity == null) {
LOGGER.warn("Inconsistent log entry (modelName: '{}', message: '{}')", severity, message);
} else {
if (emptyMessage(message)) {
LOGGER.debug("Empty message, the entry will be skipped : {}", message);
} else {
return convertDynawoLog(severity).map(severityTypedValue -> new LogEntry(severityTypedValue, message));
}
}
return Optional.empty();
}

private static Optional<TypedValue> convertDynawoLog(String severity) {
return switch (severity) {
case "DEBUG" -> Optional.of(TypedValue.DEBUG_SEVERITY);
case "INFO" -> Optional.of(TypedValue.INFO_SEVERITY);
case "WARN" -> Optional.of(TypedValue.WARN_SEVERITY);
case "ERROR" -> Optional.of(TypedValue.ERROR_SEVERITY);
default -> {
LOGGER.warn("Inconsistent severity entry '{}'", severity);
yield Optional.empty();
}
};
}

private static boolean emptyMessage(String message) {
return message == null || message.startsWith(EMPTY_BREAKER) || message.startsWith(EMPTY_START);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.xml.NetworkXml;
import com.powsybl.iidm.serde.NetworkSerDe;

import java.util.EnumMap;
import java.util.List;
Expand All @@ -33,7 +33,7 @@ private LoadsMerger() {
}

public static Network mergeLoads(Network network) throws PowsyblException {
Network mergedLoadsNetwork = NetworkXml.copy(network);
Network mergedLoadsNetwork = NetworkSerDe.copy(network);
mergedLoadsNetwork.getVoltageLevelStream().forEach(LoadsMerger::mergeLoadsInVoltageLevel);
return mergedLoadsNetwork;
}
Expand Down
Loading

0 comments on commit 03a55d7

Please sign in to comment.