Skip to content

Commit

Permalink
Remove duplicate from Curves timeSeries (#413)
Browse files Browse the repository at this point in the history
- Remove duplicates
- Use LinkedHashMap for curves and fsv

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte authored Dec 12, 2024
1 parent ce17eb5 commit 44bf8b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void testIeee14() {
assertEquals(27, result.getCurves().size());
DoubleTimeSeries ts1 = result.getCurve("_GEN____1_SM_generator_UStatorPu");
assertEquals("_GEN____1_SM_generator_UStatorPu", ts1.getMetadata().getName());
assertEquals(585, ts1.toArray().length);
assertEquals(192, ts1.toArray().length);
assertEquals(14, result.getFinalStateValues().size());
assertEquals(1.046227, result.getFinalStateValues().get("NETWORK__BUS___10_TN_Upu_value"));
List<TimelineEvent> timeLine = result.getTimeLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import com.powsybl.dynawo.commons.timeline.XmlTimeLineParser;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.serde.NetworkSerDe;
import com.powsybl.timeseries.DoubleTimeSeries;
import com.powsybl.timeseries.TimeSeries;
import com.powsybl.timeseries.TimeSeriesConstants;
import com.powsybl.timeseries.TimeSeriesCsvConfig;
import com.powsybl.timeseries.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -63,8 +60,8 @@ public final class DynawoSimulationHandler extends AbstractExecutionHandler<Dyna
private final ReportNode reportNode;

private final List<TimelineEvent> timeline = new ArrayList<>();
private final Map<String, DoubleTimeSeries> curves = new HashMap<>();
private final Map<String, Double> fsv = new HashMap<>();
private final Map<String, DoubleTimeSeries> curves = new LinkedHashMap<>();
private final Map<String, Double> fsv = new LinkedHashMap<>();
private DynamicSimulationResult.Status status = DynamicSimulationResult.Status.SUCCESS;
private String statusText = "";

Expand Down Expand Up @@ -186,14 +183,22 @@ private void setCurves(Path workingDir) {
Path curvesPath = workingDir.resolve(CURVES_OUTPUT_PATH).resolve(CURVES_FILENAME);
if (Files.exists(curvesPath)) {
TimeSeries.parseCsv(curvesPath, new TimeSeriesCsvConfig(TimeSeriesConstants.DEFAULT_SEPARATOR, false, TimeSeries.TimeFormat.FRACTIONS_OF_SECOND))
.values().forEach(l -> l.forEach(curve -> curves.put(curve.getMetadata().getName(), (DoubleTimeSeries) curve)));
.values().forEach(l -> l.forEach(curve -> curves.put(curve.getMetadata().getName(), sanitizeDoubleTimeSeries((DoubleTimeSeries) curve))));
} else {
LOGGER.warn("Curves folder not found");
status = DynamicSimulationResult.Status.FAILURE;
statusText = "Dynawo curves folder not found";
}
}

private DoubleTimeSeries sanitizeDoubleTimeSeries(DoubleTimeSeries series) {
Set<Long> times = new LinkedHashSet<>();
double[] values = series.stream().filter(dp -> times.add(dp.getTime())).mapToDouble(DoublePoint::getValue).toArray();
return TimeSeries.createDouble(series.getMetadata().getName(),
new IrregularTimeSeriesIndex(times.stream().mapToLong(l -> l).toArray()),
values);
}

private void setFinalStateValues(Path workingDir) {
Path fsvPath = workingDir.resolve(FSV_OUTPUT_PATH).resolve(FSV_OUTPUT_FILENAME);
if (Files.exists(fsvPath)) {
Expand Down

0 comments on commit 44bf8b8

Please sign in to comment.