-
Notifications
You must be signed in to change notification settings - Fork 3
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
Refactor OpenReac optimization tests #83
Open
p-arvy
wants to merge
12
commits into
main
Choose a base branch
from
refactor-open-reac-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b767132
Divide OpenReacRunnerTest into several TUs
p-arvy 8da89b0
Add TUs for ACOPF indicators
p-arvy 50548d2
Add tests on equipment bounds in optimization of OpenReac
p-arvy 802dd1d
Improve tests on ouput indicators of OpenReac
p-arvy 81908d8
Clean
p-arvy f6746be
Add resources
p-arvy 28047a0
Merge branch 'refs/heads/main' into refactor-open-reac-tests
p-arvy 84e5da6
Clean
p-arvy a54206b
Clean code adding new TUs
p-arvy cd043e8
Clean
p-arvy 1f1c0dd
Merge branch 'refs/heads/main' into refactor-open-reac-tests
p-arvy 3a3b1fd
Merge branch 'refs/heads/main' into refactor-open-reac-tests
p-arvy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
520 changes: 0 additions & 520 deletions
520
open-reac/src/test/java/com/powsybl/openreac/OpenReacRunnerTest.java
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
open-reac/src/test/java/com/powsybl/openreac/optimization/AbstractOpenReacRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* 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/. | ||
*/ | ||
package com.powsybl.openreac.optimization; | ||
|
||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
import com.powsybl.commons.report.ReportNode; | ||
import com.powsybl.commons.test.ComparisonUtils; | ||
import com.powsybl.computation.ComputationManager; | ||
import com.powsybl.computation.local.LocalCommandExecutor; | ||
import com.powsybl.computation.local.LocalComputationConfig; | ||
import com.powsybl.computation.local.LocalComputationManager; | ||
import com.powsybl.iidm.network.*; | ||
import com.powsybl.loadflow.LoadFlow; | ||
import com.powsybl.loadflow.LoadFlowResult; | ||
import com.powsybl.openreac.OpenReacConfig; | ||
import com.powsybl.openreac.OpenReacRunner; | ||
import com.powsybl.openreac.parameters.input.OpenReacParameters; | ||
import com.powsybl.openreac.parameters.output.OpenReacResult; | ||
import com.powsybl.openreac.parameters.output.OpenReacStatus; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.nio.file.Files.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>} | ||
* @author Nicolas PIERRE {@literal <nicolas.pierre at artelys.com>} | ||
*/ | ||
abstract class AbstractOpenReacRunnerTest { | ||
protected FileSystem fileSystem; | ||
protected Path tmpDir; | ||
|
||
@BeforeEach | ||
public void setUp() throws IOException { | ||
fileSystem = Jimfs.newFileSystem(Configuration.unix()); | ||
tmpDir = createDirectory(fileSystem.getPath("tmp")); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws IOException { | ||
fileSystem.close(); | ||
} | ||
|
||
protected void assertEqualsToRef(Path p, String refFileName) throws IOException { | ||
try (InputStream actual = newInputStream(p)) { | ||
ComparisonUtils.assertTxtEquals(Objects.requireNonNull(getClass().getResourceAsStream(refFileName)), actual); | ||
} | ||
} | ||
|
||
protected Path getAmplExecPath() throws IOException { | ||
Path execFolder; | ||
try (Stream<Path> walk = walk(tmpDir)) { | ||
execFolder = walk.limit(2).collect(Collectors.toList()).get(1); | ||
} | ||
return execFolder; | ||
} | ||
|
||
/** | ||
* Runs OpenReac, apply the results and verify that a load flow converges on these results. | ||
*/ | ||
protected void testAllModifAndLoadFlow(Network network, String subFolder, OpenReacParameters parameters, ReportNode reportNode) throws IOException { | ||
runAndApplyAllModifications(network, subFolder, parameters, true, reportNode); | ||
LoadFlowResult loadFlowResult = LoadFlow.run(network); | ||
assertTrue(loadFlowResult.isFullyConverged()); | ||
} | ||
|
||
/** | ||
* Runs OpenReac and apply the results on the network. | ||
*/ | ||
void runAndApplyAllModifications(Network network, String subFolder, OpenReacParameters parameters, | ||
boolean updateNetworkWithVoltages, ReportNode reportNode) throws IOException { | ||
// set default voltage limits to every voltage levels of the network | ||
setDefaultVoltageLimits(network); | ||
LocalCommandExecutor localCommandExecutor = new TestLocalCommandExecutor( | ||
List.of(subFolder + "/reactiveopf_results_generators.csv", | ||
subFolder + "/reactiveopf_results_indic.txt", | ||
subFolder + "/reactiveopf_results_rtc.csv", | ||
subFolder + "/reactiveopf_results_shunts.csv", | ||
subFolder + "/reactiveopf_results_static_var_compensators.csv", | ||
subFolder + "/reactiveopf_results_vsc_converter_stations.csv", | ||
subFolder + "/reactiveopf_results_voltages.csv")); | ||
// To really run open reac, use the commentede line below. Be sure that open-reac/src/test/resources/com/powsybl/config/test/config.yml contains your ampl path | ||
// try (ComputationManager computationManager = new LocalComputationManager()) { | ||
try (ComputationManager computationManager = new LocalComputationManager(new LocalComputationConfig(tmpDir), | ||
localCommandExecutor, ForkJoinPool.commonPool())) { | ||
OpenReacResult openReacResult = OpenReacRunner.run(network, | ||
network.getVariantManager().getWorkingVariantId(), parameters, | ||
new OpenReacConfig(true), computationManager, reportNode, null); | ||
assertEquals(OpenReacStatus.OK, openReacResult.getStatus()); | ||
openReacResult.setUpdateNetworkWithVoltages(updateNetworkWithVoltages); | ||
openReacResult.applyAllModifications(network); | ||
} | ||
} | ||
|
||
/** | ||
* Add voltage limits to voltage levels with undefined limits. | ||
* OpenReac needs voltage limits to run optimization. | ||
*/ | ||
void setDefaultVoltageLimits(Network network) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark, why not adding the |
||
for (VoltageLevel vl : network.getVoltageLevels()) { | ||
if (vl.getLowVoltageLimit() <= 0 || Double.isNaN(vl.getLowVoltageLimit())) { | ||
vl.setLowVoltageLimit(0.5 * vl.getNominalV()); | ||
} | ||
if (Double.isNaN(vl.getHighVoltageLimit())) { | ||
vl.setHighVoltageLimit(1.5 * vl.getNominalV()); | ||
} | ||
} | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
open-reac/src/test/java/com/powsybl/openreac/optimization/OpenReacAmplIOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* 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/. | ||
*/ | ||
package com.powsybl.openreac.optimization; | ||
|
||
import com.powsybl.computation.ComputationManager; | ||
import com.powsybl.computation.local.LocalCommandExecutor; | ||
import com.powsybl.computation.local.LocalComputationConfig; | ||
import com.powsybl.computation.local.LocalComputationManager; | ||
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory; | ||
import com.powsybl.iidm.network.*; | ||
import com.powsybl.openreac.OpenReacConfig; | ||
import com.powsybl.openreac.OpenReacRunner; | ||
import com.powsybl.openreac.parameters.input.OpenReacParameters; | ||
import com.powsybl.openreac.parameters.input.algo.OpenReacOptimisationObjective; | ||
import com.powsybl.openreac.parameters.input.algo.ReactiveSlackBusesMode; | ||
import com.powsybl.openreac.parameters.output.OpenReacResult; | ||
import com.powsybl.openreac.parameters.output.OpenReacStatus; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>} | ||
* @author Nicolas PIERRE {@literal <nicolas.pierre at artelys.com>} | ||
*/ | ||
class OpenReacAmplIOTest extends AbstractOpenReacRunnerTest { | ||
|
||
@Test | ||
void testInputFile() throws IOException { | ||
Network network = IeeeCdfNetworkFactory.create57(); | ||
setDefaultVoltageLimits(network); // set default voltage limits to every voltage levels of the network | ||
|
||
OpenReacParameters parameters = new OpenReacParameters().setObjective( | ||
OpenReacOptimisationObjective.BETWEEN_HIGH_AND_LOW_VOLTAGE_LIMIT) | ||
.setObjectiveDistance(70) | ||
.setReactiveSlackBusesMode(ReactiveSlackBusesMode.CONFIGURED) | ||
.addVariableTwoWindingsTransformers(network.getTwoWindingsTransformerStream() | ||
.limit(1) | ||
.map(TwoWindingsTransformer::getId) | ||
.collect(Collectors.toList())) | ||
.addConstantQGenerators( | ||
network.getGeneratorStream().limit(1).map(Generator::getId).collect(Collectors.toList())) | ||
.addVariableShuntCompensators( | ||
network.getShuntCompensatorStream().limit(1).map(ShuntCompensator::getId).collect(Collectors.toList())) | ||
.addConfiguredReactiveSlackBuses( | ||
network.getBusView().getBusStream().limit(1).map(Bus::getId).collect(Collectors.toList())); | ||
|
||
LocalCommandExecutor localCommandExecutor = new TestLocalCommandExecutor( | ||
List.of("empty_case/reactiveopf_results_indic.txt")); | ||
try (ComputationManager computationManager = new LocalComputationManager(new LocalComputationConfig(tmpDir), | ||
localCommandExecutor, ForkJoinPool.commonPool())) { | ||
OpenReacRunner.run(network, network.getVariantManager().getWorkingVariantId(), parameters, | ||
new OpenReacConfig(true), computationManager); | ||
Path execFolder = getAmplExecPath(); | ||
assertEqualsToRef(execFolder.resolve("param_algo.txt"), "/expected_inputs/param_algo.txt"); | ||
assertEqualsToRef(execFolder.resolve("param_generators_reactive.txt"), "/expected_inputs/param_generators_reactive.txt"); | ||
assertEqualsToRef(execFolder.resolve("param_shunts.txt"), "/expected_inputs/param_shunts.txt"); | ||
assertEqualsToRef(execFolder.resolve("param_transformers.txt"), "/expected_inputs/param_transformers.txt"); | ||
assertEqualsToRef(execFolder.resolve("param_buses_with_reactive_slack.txt"), "/expected_inputs/param_buses_with_reactive_slack.txt"); | ||
} | ||
} | ||
|
||
@Test | ||
void testOutputFileParsing() throws IOException { | ||
Network network = IeeeCdfNetworkFactory.create57(); | ||
setDefaultVoltageLimits(network); // set default voltage limits to every voltage levels of the network | ||
// To parse correctly data from output files, there must be an ID in the Ampl mapper | ||
// For this we add dummy elements to the network, | ||
// they will get exported, but the ampl mapper will have IDs for them. | ||
// All the values are bad, and they are not used. | ||
// RTC | ||
network.getTwoWindingsTransformerStream() | ||
.forEach(t -> t.newRatioTapChanger() | ||
.setLowTapPosition(0) | ||
.setTapPosition(0) | ||
.beginStep() | ||
.setR(0.01) | ||
.setX(0.0001) | ||
.setB(0) | ||
.setG(0) | ||
.setRho(1.1) | ||
.endStep() | ||
.add()); | ||
// SVC | ||
VoltageLevel vl = network.getVoltageLevelStream().iterator().next(); | ||
vl.getBusBreakerView().newBus().setId("bus-1").add(); | ||
vl.newStaticVarCompensator() | ||
.setId("dummyStaticVarCompensator") | ||
.setBus("bus-1") | ||
.setBmin(1.1) | ||
.setBmax(1.3) | ||
.setRegulationMode(StaticVarCompensator.RegulationMode.OFF) | ||
.add(); | ||
// VSC | ||
vl.newVscConverterStation() | ||
.setId("dummyVscConverterStation") | ||
.setConnectableBus("bus-1") | ||
.setBus("bus-1") | ||
.setLossFactor(1.1f) | ||
.setVoltageSetpoint(405.0) | ||
.setVoltageRegulatorOn(true) | ||
.add(); | ||
|
||
LocalCommandExecutor localCommandExecutor = new TestLocalCommandExecutor( | ||
List.of("mock_outputs/reactiveopf_results_generators.csv", | ||
"mock_outputs/reactiveopf_results_indic.txt", | ||
"mock_outputs/reactiveopf_results_rtc.csv", | ||
"mock_outputs/reactiveopf_results_shunts.csv", | ||
"mock_outputs/reactiveopf_results_static_var_compensators.csv", | ||
"mock_outputs/reactiveopf_results_vsc_converter_stations.csv", | ||
"mock_outputs/reactiveopf_results_voltages.csv")); | ||
try (ComputationManager computationManager = new LocalComputationManager(new LocalComputationConfig(tmpDir), | ||
localCommandExecutor, ForkJoinPool.commonPool())) { | ||
OpenReacResult openReacResult = OpenReacRunner.run(network, | ||
network.getVariantManager().getWorkingVariantId(), new OpenReacParameters(), new OpenReacConfig(true), | ||
computationManager); | ||
|
||
assertEquals(OpenReacStatus.OK, openReacResult.getStatus()); | ||
assertEquals(1, openReacResult.getShuntsModifications().size()); | ||
assertEquals(2, openReacResult.getTapPositionModifications().size()); | ||
assertEquals(1, openReacResult.getSvcModifications().size()); | ||
assertEquals(1, openReacResult.getVscModifications().size()); | ||
assertEquals(7, openReacResult.getGeneratorModifications().size()); | ||
assertEquals(3, openReacResult.getVoltageProfile().size()); | ||
assertEquals(87, openReacResult.getIndicators().size()); | ||
assertTrue(openReacResult.getReactiveSlacks().isEmpty()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not adding the
protected
keyword here?