Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump core 6.0.1 #34

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions open-reac/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<description>OpenReac optimizer</description>

<properties>
<powsybl-core.version>5.3.2</powsybl-core.version>
<powsybl-open-loadflow.version>1.2.1</powsybl-open-loadflow.version>
<powsybl-core.version>6.0.1</powsybl-core.version>
<powsybl-open-loadflow.version>1.3.0</powsybl-open-loadflow.version>
</properties>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import com.powsybl.commons.util.StringToIntMapper;
import com.powsybl.openreac.parameters.AmplIOUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;

/**
Expand All @@ -28,18 +27,17 @@ protected AbstractElementsInput(List<String> elementIds) {
}

@Override
public InputStream getParameterFileAsStream(StringToIntMapper<AmplSubset> stringToIntMapper) {
StringBuilder dataBuilder = new StringBuilder();
dataBuilder.append("#amplId powsyblId\n");
public void write(BufferedWriter writer, StringToIntMapper<AmplSubset> stringToIntMapper) throws IOException {
writer.write("#amplId powsyblId\n");
for (String elementID : elementIds) {
int amplId = stringToIntMapper.getInt(getElementAmplSubset(), elementID);
String[] tokens = {Integer.toString(amplId), AmplIOUtils.addQuotes(elementID)};
dataBuilder.append(String.join(" ", tokens));
dataBuilder.append(System.lineSeparator());
writer.write(String.join(" ", tokens));
writer.newLine();
}
//add new line at the end of the file !
dataBuilder.append("\n");
return new ByteArrayInputStream(dataBuilder.toString().getBytes(StandardCharsets.UTF_8));
writer.newLine();
writer.flush();
}

abstract AmplSubset getElementAmplSubset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import com.powsybl.openreac.exceptions.InvalidParametersException;
import org.jgrapht.alg.util.Pair;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -75,10 +74,9 @@ public String getFileName() {
}

@Override
public InputStream getParameterFileAsStream(StringToIntMapper<AmplSubset> stringToIntMapper) {
StringBuilder dataBuilder = new StringBuilder();
dataBuilder.append("#num minV (pu) maxV (pu) id");
dataBuilder.append(System.lineSeparator());
public void write(BufferedWriter writer, StringToIntMapper<AmplSubset> stringToIntMapper) throws IOException {
writer.write("#num minV (pu) maxV (pu) id");
writer.newLine();

for (Map.Entry<String, Pair<Double, Double>> entry : normalizedVoltageLimitsOverride.entrySet()) {
String voltageLevelId = entry.getKey();
Expand All @@ -87,13 +85,13 @@ public InputStream getParameterFileAsStream(StringToIntMapper<AmplSubset> string
if (!Double.isNaN(limits.getFirst()) || !Double.isNaN(limits.getSecond())) {
int amplId = stringToIntMapper.getInt(AmplSubset.VOLTAGE_LEVEL, voltageLevelId);
String[] tokens = {Integer.toString(amplId), Double.toString(limits.getFirst()), Double.toString(limits.getSecond()), "\"" + voltageLevelId + "\""};
dataBuilder.append(String.join(" ", tokens));
dataBuilder.append(System.lineSeparator());
writer.write(String.join(" ", tokens));
writer.newLine();
}
}

//add new line at the end of the file
dataBuilder.append(System.lineSeparator());
return new ByteArrayInputStream(dataBuilder.toString().getBytes(StandardCharsets.UTF_8));
writer.newLine();
writer.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import com.powsybl.ampl.executor.AmplInputFile;
import com.powsybl.commons.util.StringToIntMapper;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;

/**
Expand All @@ -33,13 +32,15 @@ public String getFileName() {
}

@Override
public InputStream getParameterFileAsStream(StringToIntMapper<AmplSubset> stringToIntMapper) {
StringBuilder dataBuilder = new StringBuilder();
public void write(BufferedWriter writer, StringToIntMapper<AmplSubset> stringToIntMapper) throws IOException {
for (OpenReacAlgoParam param : algoParameters) {
dataBuilder.append(param.getName()).append(" ").append(param.getValue()).append("\n");
writer.append(param.getName())
.append(" ")
.append(param.getValue());
writer.newLine();
}
//add new line at the end of the file !
dataBuilder.append("\n");
return new ByteArrayInputStream(dataBuilder.toString().getBytes(StandardCharsets.UTF_8));
writer.newLine();
writer.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import com.powsybl.ampl.executor.AmplOutputFile;
import com.powsybl.commons.util.StringToIntMapper;

import java.nio.file.Path;
import java.io.BufferedReader;
import java.io.IOException;

/**
* Interface for output ampl files, which changes the behavior of errors during file reading.
Expand All @@ -31,7 +32,7 @@ public interface NoThrowAmplOutput extends AmplOutputFile {
* </ul>
*/
@Override
void read(Path path, StringToIntMapper<AmplSubset> stringToIntMapper);
void read(BufferedReader var1, StringToIntMapper<AmplSubset> var2) throws IOException;

/**
* @return True if the output reading went bad. WARNING, object might have undefined behavior.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import com.powsybl.openreac.exceptions.IncompatibleModelException;
import com.powsybl.openreac.parameters.AmplIOUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -65,30 +63,27 @@ public String getFileName() {
}

@Override
public void read(Path path, StringToIntMapper<AmplSubset> amplMapper) {
List<String> reactiveSlackLines;
// if the file is missing, we know there is no reactive slack.
if (Files.isRegularFile(path)) {
try {
reactiveSlackLines = Files.readAllLines(path, StandardCharsets.UTF_8);
} catch (IOException e) {
// File reading went wrong
triggerErrorState();
return;
}
String headers = reactiveSlackLines.get(0);
int readCols = headers.split(SEP).length;
if (readCols != EXPECTED_COLS) {
triggerErrorState();
throw new IncompatibleModelException("Error reading " + getFileName() + ", wrong number of columns. Expected: " + EXPECTED_COLS + ", found:" + readCols);
} else {
for (String line : reactiveSlackLines.subList(1, reactiveSlackLines.size())) {
readLine(line.split(SEP));
}
public void read(BufferedReader reader, StringToIntMapper<AmplSubset> stringToIntMapper) throws IOException {
String headers = reader.readLine();
int readCols = headers.split(SEP).length;
if (readCols != EXPECTED_COLS) {
triggerErrorState();
throw new IncompatibleModelException("Error reading " + getFileName() + ", wrong number of columns. Expected: " + EXPECTED_COLS + ", found:" + readCols);
} else {
String line = reader.readLine();
while (line != null) {
readLine(line.split(SEP));
line = reader.readLine();
}
}
}

@Override
public boolean throwOnMissingFile() {
// if the file is missing, we know there is no reactive slack.
return false;
}

private void readLine(String[] tokens) {
// slack capacitor is a generation of reactive power.
// slack self is a reactive load.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import com.powsybl.openreac.OpenReacModel;
import com.powsybl.openreac.parameters.output.AbstractNoThrowOutput;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -43,25 +42,23 @@ public String getFileName() {
}

@Override
public void read(Path path, StringToIntMapper<AmplSubset> stringToIntMapper) {
List<String> lines = null;
try {
lines = Files.readAllLines(path);
} catch (IOException e) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a little difference here.

Previously, no exception was thrown when an IOException was thrown during readAllLines .

Now, no exception is thrown when the file is not a regular file (real file + read access on it), but if another IOException is thrown, it will be propagated as an UncheckedIOException by the caller (see AmplModelExecutionHandler.readCustomFiles in powsybl-core). But I don't know if it can really happen.

triggerErrorState();
}
if (lines != null) {
for (String line : lines) {
if (!COMMENTED_LINE_TEST.test(line)) {
String[] tokens = line.split(OpenReacModel.OUTPUT_FILE_FORMAT.getTokenSeparator());
modifications.add(doReadLine(tokens, stringToIntMapper));
}
public void read(BufferedReader bufferedReader, StringToIntMapper<AmplSubset> stringToIntMapper) throws IOException {
bufferedReader.lines().forEach(line -> {
if (!COMMENTED_LINE_TEST.test(line)) {
String[] tokens = line.split(OpenReacModel.OUTPUT_FILE_FORMAT.getTokenSeparator());
modifications.add(doReadLine(tokens, stringToIntMapper));
}
}
});
}

protected abstract T doReadLine(String[] tokens, StringToIntMapper<AmplSubset> stringToIntMapper);

@Override
public boolean throwOnMissingFile() {
triggerErrorState();
return false;
}

public List<T> getModifications() {
return modifications;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.openreac.parameters.input;

import com.google.common.io.ByteStreams;
import com.powsybl.ampl.converter.AmplSubset;
import com.powsybl.ampl.converter.AmplUtil;
import com.powsybl.commons.PowsyblException;
Expand All @@ -18,12 +17,15 @@
import com.powsybl.openreac.exceptions.InvalidParametersException;
import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

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

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand All @@ -46,8 +48,10 @@ void testValidRelativeVoltageOverride() throws IOException {

VoltageLevelLimitsOverrideInput input = new VoltageLevelLimitsOverrideInput(voltageLimitsOverride, network);
StringToIntMapper<AmplSubset> mapper = AmplUtil.createMapper(network);
try (var is = input.getParameterFileAsStream(mapper)) {
String data = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
try (Writer w = new StringWriter();
BufferedWriter writer = new BufferedWriter(w)) {
input.write(writer, mapper);
String data = w.toString();
String ref = String.join(System.lineSeparator(), "#num minV (pu) maxV (pu) id",
"1 0.7916666666666667 1.1666666666666665 \"VLGEN\"") + System.lineSeparator() + System.lineSeparator();
assertEquals(ref, data);
Expand All @@ -70,8 +74,11 @@ void testValidAbsoluteVoltageOverride() throws IOException {

VoltageLevelLimitsOverrideInput input = new VoltageLevelLimitsOverrideInput(voltageLimitsOverride, network);
StringToIntMapper<AmplSubset> mapper = AmplUtil.createMapper(network);
try (var is = input.getParameterFileAsStream(mapper)) {
String data = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);

try (Writer w = new StringWriter();
BufferedWriter writer = new BufferedWriter(w)) {
input.write(writer, mapper);
String data = w.toString();
String ref = String.join(System.lineSeparator(), "#num minV (pu) maxV (pu) id",
"1 0.8333333333333334 1.0833333333333333 \"VLGEN\"") + System.lineSeparator() + System.lineSeparator();
assertEquals(ref, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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.openreac.parameters.output;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

/**
* @author Olivier Perrin {@literal <olivier.perrin at rte-france.com>}
*/
class ReactiveSlackOutputTest {

@Test
void readTest() throws IOException {
ReactiveSlackOutput output = new ReactiveSlackOutput();
try (InputStream input = getClass().getResourceAsStream("/mock_outputs/reactiveopf_results_reactive_slacks.csv");
InputStreamReader in = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(in)) {

output.read(reader, null);
assertEquals(2, output.getSlacks().size());
ReactiveSlackOutput.ReactiveSlack slack1 = output.getSlacks().get(0);
ReactiveSlackOutput.ReactiveSlack slack2 = output.getSlacks().get(1);
Assertions.assertAll(
() -> assertEquals(1.6, slack1.getSlack(), 0.01),
() -> assertEquals("slack1", slack1.getBusId()),
() -> assertEquals("voltageLevel1", slack1.getVoltageLevelId()),
() -> assertEquals(Double.NaN, slack2.getSlack()),
() -> assertEquals("slack2", slack2.getBusId()),
() -> assertEquals("voltageLevel2", slack2.getVoltageLevelId())
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
col1;col2;col3;col4;col5;col6;
'a1';'b1';5.3;6.9;'slack1';'voltageLevel1';
'a2';'b2';1.2;-99999;'slack2';'voltageLevel2';
Loading