Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Nov 3, 2023
1 parent 06812f5 commit b200dbb
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
this.parameters = Objects.requireNonNull(parameters);
this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters);

Iterator<DataOptimizer> dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(DataOptimizer.class).iterator() : Collections.emptyIterator();
Iterator<ModelOptimizer> dataOptimizers = dynaWaltzParameters.isUseModelOptimizers() ? ServiceLoader.load(ModelOptimizer.class).iterator() : Collections.emptyIterator();
this.dynamicModels = runOptimizers(dataOptimizers,
Objects.requireNonNull(dynamicModels)
.stream()
Expand Down Expand Up @@ -106,7 +106,7 @@ private FrequencySynchronizerModel setupFrequencySynchronizer(Function<List<Freq
.toList());
}

private Stream<BlackBoxModel> runOptimizers(Iterator<DataOptimizer> dataOptimizers, Stream<BlackBoxModel> inputData, Reporter reporter) {
private Stream<BlackBoxModel> runOptimizers(Iterator<ModelOptimizer> dataOptimizers, Stream<BlackBoxModel> inputData, Reporter reporter) {
return dataOptimizers.hasNext() ? runOptimizers(dataOptimizers, dataOptimizers.next().optimizeModels(inputData, dynaWaltzParameters, reporter), reporter) : inputData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DynaWaltzParameters extends AbstractExtension<DynamicSimulationPara
public static final String NETWORK_OUTPUT_PARAMETERS_FILE = "network.par";
public static final String SOLVER_OUTPUT_PARAMETERS_FILE = "solvers.par";
private static final boolean DEFAULT_WRITE_FINAL_STATE = true;
private static final boolean DEFAULT_USE_MODEL_OPTIMIZERS = false;
public static final boolean DEFAULT_USE_MODEL_OPTIMIZERS = false;

public enum SolverType {
SIM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public interface DataOptimizer {
public interface ModelOptimizer {

Stream<BlackBoxModel> optimizeModels(Stream<BlackBoxModel> models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter);

default Stream<BlackBoxModel> optimizeModels(Stream<BlackBoxModel> models, DynaWaltzParameters dynaWaltzParameters) {
return optimizeModels(models, dynaWaltzParameters, Reporter.NO_OP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ void checkParametersNotFound() {
UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> ParametersXml.load(path));
assertEquals("java.nio.file.NoSuchFileException: /file.par", e.getMessage());
}

@Test
void addParametersSet() {
DynaWaltzParameters dParameters = new DynaWaltzParameters();
ParametersSet set = new ParametersSet("test");
dParameters.addModelParameters(set);
assertEquals(1, dParameters.getModelParameters().size());
assertEquals(set, dParameters.getModelParameters("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void checkParameters() throws IOException {
SolverType solverType = SolverType.IDA;
String solverParametersId = "solverParametersId";
boolean mergeLoads = true;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads);
boolean useModelOptimizers = true;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelOptimizers);

DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem);

Expand Down Expand Up @@ -87,6 +88,7 @@ void checkParameters() throws IOException {
assertEquals(ParameterType.DOUBLE, absAccuracy.type());

assertEquals(mergeLoads, parameters.isMergeLoads());
assertEquals(useModelOptimizers, parameters.isUseModelOptimizers());
}

@Test
Expand All @@ -95,7 +97,7 @@ void roundTripParametersSerializing() throws IOException {
SolverType solverType = SolverType.IDA;
String solverParametersId = "solverParametersId";
boolean mergeLoads = false;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads);
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false);

DynamicSimulationParameters dynamicSimulationParameters = new DynamicSimulationParameters()
.setStartTime(0)
Expand All @@ -106,7 +108,7 @@ void roundTripParametersSerializing() throws IOException {
JsonDynamicSimulationParameters::read, "/DynaWaltzParameters.json");
}

private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads) throws IOException {
private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelOptimizers) throws IOException {
String parametersFile = USER_HOME + "parametersFile";
String networkParametersFile = USER_HOME + "networkParametersFile";
String solverParametersFile = USER_HOME + "solverParametersFile";
Expand All @@ -119,6 +121,7 @@ private void initPlatformConfig(String networkParametersId, SolverType solverTyp
moduleConfig.setStringProperty("network.parametersId", networkParametersId);
moduleConfig.setStringProperty("solver.type", solverType.toString());
moduleConfig.setStringProperty("solver.parametersId", solverParametersId);
moduleConfig.setStringProperty("useModelOptimizers", String.valueOf(useModelOptimizers));

Files.createDirectories(fileSystem.getPath(USER_HOME));
copyFile("/parametersSet/models.par", parametersFile);
Expand All @@ -145,6 +148,7 @@ void checkDefaultParameters() throws IOException {
assertEquals("1", parameters.getSolverParameters().getId());

assertEquals(DynaWaltzParameters.DEFAULT_MERGE_LOADS, parameters.isMergeLoads());
assertEquals(DynaWaltzParameters.DEFAULT_USE_MODEL_OPTIMIZERS, parameters.isUseModelOptimizers());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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.dynawaltz;

import com.google.auto.service.AutoService;
import com.google.common.collect.Lists;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.dynamicsimulation.DynamicSimulationParameters;
import com.powsybl.dynawaltz.models.BlackBoxModel;
import com.powsybl.dynawaltz.models.generators.AbstractGenerator;
import com.powsybl.dynawaltz.models.generators.GeneratorFictitious;
import com.powsybl.dynawaltz.models.loads.BaseLoad;
import com.powsybl.dynawaltz.models.transformers.TransformerFixedRatio;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Stream;

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

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

@Test
void loadOptimizer() {
List<ModelOptimizer> optimizers = Lists.newArrayList(ServiceLoader.load(ModelOptimizer.class));
assertEquals(2, optimizers.size());
}

@Test
void optimizeModels() {
Network network = EurostagTutorialExample1Factory.create();
DynamicSimulationParameters parameters = DynamicSimulationParameters.load();
DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load().setUseModelOptimizers(true);
List<BlackBoxModel> dynamicModels = List.of(
new GeneratorFictitious("BBM_GEN", network.getGenerator("GEN"), "GPV"),
new BaseLoad("BBM_LOAD", network.getLoad("LOAD"), "LOAD", "LoadAlphaBeta"),
new TransformerFixedRatio("BBM_TRA", network.getTwoWindingsTransformer("NGEN_NHV1"), "TR", "TransformerFixedRatio"));
DynaWaltzContext context = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, Collections.emptyList(), Collections.emptyList(), parameters, dynawoParameters);
assertEquals(2, context.getBlackBoxDynamicModels().size());
assertFalse(context.getBlackBoxDynamicModelStream().anyMatch(bbm -> bbm.getDynamicModelId().equalsIgnoreCase("BBM_LOAD")));
assertTrue(context.getBlackBoxDynamicModelStream().anyMatch(bbm -> bbm.getDynamicModelId().equalsIgnoreCase("newModel")));
}

@AutoService(ModelOptimizer.class)
public static class ModelOptimizerFilter implements ModelOptimizer {
@Override
public Stream<BlackBoxModel> optimizeModels(Stream<BlackBoxModel> models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) {
return models.filter(m -> !m.getDynamicModelId().equalsIgnoreCase("BBM_LOAD"));
}
}

@AutoService(ModelOptimizer.class)
public static class ModelOptimizerSubstitution implements ModelOptimizer {
@Override
public Stream<BlackBoxModel> optimizeModels(Stream<BlackBoxModel> models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter) {
return models.map(m -> {
if ("BBM_GEN".equalsIgnoreCase(m.getDynamicModelId()) && m instanceof AbstractGenerator gen) {
return new GeneratorFictitious("newModel", gen.getEquipment(), "G");
}
return m;
});
}
}
}
45 changes: 45 additions & 0 deletions dynawaltz/src/test/java/com/powsybl/dynawaltz/models/HvdcTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.dynawaltz.models;

import com.powsybl.dynawaltz.models.hvdc.HvdcP;
import com.powsybl.dynawaltz.models.hvdc.HvdcPDangling;
import com.powsybl.dynawaltz.models.hvdc.HvdcVscDangling;
import com.powsybl.iidm.network.HvdcLine;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.HvdcTestNetwork;
import org.junit.jupiter.api.Test;

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

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

@Test
void testConnectedStation() {
Network network = HvdcTestNetwork.createVsc();
HvdcP hvdc = new HvdcP("hvdc", network.getHvdcLine("L"), "HVDC", "HvdcPV");
assertEquals(2, hvdc.getConnectedStations().size());
}

@Test
void testDanglingConnectedStation() {
Network network = HvdcTestNetwork.createVsc();
HvdcLine line = network.getHvdcLine("L");

HvdcPDangling hvdc = new HvdcPDangling("hvdc", line, "HVDC", "HvdcPVDangling", Side.ONE);
assertEquals(1, hvdc.getConnectedStations().size());
assertEquals(line.getConverterStation2(), hvdc.getConnectedStations().get(0));

HvdcVscDangling hvdc2 = new HvdcVscDangling("hvdc", line, "HVDC", "HvdcVSCDanglingP", Side.TWO);
assertEquals(1, hvdc.getConnectedStations().size());
assertEquals(line.getConverterStation1(), hvdc2.getConnectedStations().get(0));
}
}

0 comments on commit b200dbb

Please sign in to comment.