Skip to content

Commit

Permalink
Finish powsybl-core 6.0.1 bump
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Perrin <olivier.perrin@rte-france.com>
  • Loading branch information
olperr1 committed Oct 19, 2023
1 parent d738368 commit d7bade1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
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 @@ -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 @@ -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

0 comments on commit d7bade1

Please sign in to comment.