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

Handle optimizers #302

Merged
merged 14 commits into from
Dec 13, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,30 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
Reporter contextReporter = DynawaltzReports.createDynaWaltzContextReporter(reporter);
this.network = Objects.requireNonNull(network);
this.workingVariantId = Objects.requireNonNull(workingVariantId);
this.parameters = Objects.requireNonNull(parameters);
this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters);

this.dynamicModels = Objects.requireNonNull(dynamicModels).stream()
.filter(distinctByDynamicId(contextReporter).and(distinctByStaticId(contextReporter)))
Stream<BlackBoxModel> uniqueIdsDynamicModels = Objects.requireNonNull(dynamicModels).stream()
.filter(distinctByDynamicId(contextReporter).and(distinctByStaticId(contextReporter)));
this.dynamicModels = dynaWaltzParameters.isUseModelSimplifiers()
? simplifyModels(uniqueIdsDynamicModels, contextReporter).toList()
: uniqueIdsDynamicModels.toList();

this.eventModels = Objects.requireNonNull(eventModels).stream()
.filter(distinctByDynamicId(contextReporter))
.toList();
this.staticIdBlackBoxModelMap = getInputBlackBoxDynamicModelStream()
.filter(EquipmentBlackBoxModel.class::isInstance)
.map(EquipmentBlackBoxModel.class::cast)
.collect(Collectors.toMap(EquipmentBlackBoxModel::getStaticId, Function.identity()));

this.eventModels = Objects.requireNonNull(eventModels).stream()
.filter(distinctByDynamicId(contextReporter))
.toList();

// Late init on ContextDependentEvents
this.eventModels.stream()
.filter(ContextDependentEvent.class::isInstance)
.map(ContextDependentEvent.class::cast)
.forEach(e -> e.setEquipmentHasDynamicModel(this));

this.curves = Objects.requireNonNull(curves);
this.parameters = Objects.requireNonNull(parameters);
this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters);
this.frequencySynchronizer = setupFrequencySynchronizer(dynamicModels.stream().anyMatch(AbstractBus.class::isInstance) ? SetPoint::new : OmegaRef::new);
this.macroConnectionsAdder = new MacroConnectionsAdder(this::getDynamicModel,
this::getPureDynamicModel,
Expand All @@ -117,6 +119,14 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
}
}

private Stream<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> inputBbm, Reporter reporter) {
Stream<BlackBoxModel> outputBbm = inputBbm;
for (ModelsSimplifier modelsSimplifier : ServiceLoader.load(ModelsSimplifier.class)) {
outputBbm = modelsSimplifier.simplifyModels(outputBbm, dynaWaltzParameters, reporter);
}
return outputBbm;
}

private FrequencySynchronizerModel setupFrequencySynchronizer(Function<List<FrequencySynchronizedModel>, FrequencySynchronizerModel> fsConstructor) {
return fsConstructor.apply(dynamicModels.stream()
.filter(FrequencySynchronizedModel.class::isInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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;
public static final boolean USE_MODEL_SIMPLIFIERS = false;

public enum SolverType {
SIM,
Expand All @@ -51,6 +52,7 @@ public enum SolverType {
private SolverType solverType;
private boolean mergeLoads;
private boolean writeFinalState = DEFAULT_WRITE_FINAL_STATE;
private boolean useModelSimplifiers = USE_MODEL_SIMPLIFIERS;
private DumpFileParameters dumpFileParameters;

/**
Expand Down Expand Up @@ -102,6 +104,8 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem
// Writes final state IIDM
boolean writeFinalState = config.flatMap(c -> c.getOptionalBooleanProperty("writeFinalState")).orElse(DEFAULT_WRITE_FINAL_STATE);

boolean useModelSimplifiers = config.flatMap(c -> c.getOptionalBooleanProperty("useModelSimplifiers")).orElse(USE_MODEL_SIMPLIFIERS);

// Dump file config
boolean exportDumpFile = config.flatMap(c -> c.getOptionalBooleanProperty("dump.export")).orElse(DumpFileParameters.DEFAULT_EXPORT_DUMP);
String exportDumpFileFolder = config.flatMap(c -> c.getOptionalStringProperty("dump.exportFolder")).orElse(DumpFileParameters.DEFAULT_DUMP_FOLDER);
Expand Down Expand Up @@ -130,6 +134,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem
.setSolverType(solverType)
.setMergeLoads(mergeLoads)
.setWriteFinalState(writeFinalState)
.setUseModelSimplifiers(useModelSimplifiers)
.setDumpFileParameters(dumpFileParameters);
}

Expand All @@ -138,6 +143,10 @@ public String getName() {
return "DynaWaltzParameters";
}

public void addModelParameters(ParametersSet parameterSet) {
modelsParameters.put(parameterSet.getId(), parameterSet);
}

public ParametersSet getModelParameters(String parameterSetId) {
ParametersSet parametersSet = modelsParameters.get(parameterSetId);
if (parametersSet == null) {
Expand Down Expand Up @@ -203,6 +212,15 @@ public boolean isWriteFinalState() {
return writeFinalState;
}

public boolean isUseModelSimplifiers() {
return useModelSimplifiers;
}

public DynaWaltzParameters setUseModelSimplifiers(boolean useModelSimplifiers) {
this.useModelSimplifiers = useModelSimplifiers;
return this;
}

public DumpFileParameters getDumpFileParameters() {
return dumpFileParameters;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.powsybl.commons.reporter.Reporter;
import com.powsybl.dynawaltz.models.BlackBoxModel;

import java.util.stream.Stream;

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public interface ModelsSimplifier {

Stream<BlackBoxModel> simplifyModels(Stream<BlackBoxModel> models, DynaWaltzParameters dynaWaltzParameters, Reporter reporter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public abstract class AbstractBlackBoxModel implements BlackBoxModel {

private final String dynamicModelId;
private final String parameterSetId;
private String parameterSetId;

protected AbstractBlackBoxModel(String dynamicModelId, String parameterSetId) {
this.dynamicModelId = Objects.requireNonNull(dynamicModelId);
Expand All @@ -45,6 +45,10 @@ public String getParameterSetId() {
return parameterSetId;
}

public void setParameterSetId(String parameterSetId) {
this.parameterSetId = parameterSetId;
}

@Override
public void createDynamicModelParameters(DynaWaltzContext context, Consumer<ParametersSet> parametersAdder) {
// method empty by default to be redefined by specific models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public String getStaticId() {
return equipment.getId();
}

@Override
public T getEquipment() {
return equipment;
}

@Override
public String getLib() {
return lib;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
*/
package com.powsybl.dynawaltz.models;

import com.powsybl.iidm.network.Identifiable;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public interface EquipmentBlackBoxModel extends BlackBoxModel {
String getStaticId();

Identifiable<?> getEquipment();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.powsybl.dynawaltz.models.buses.EquipmentConnectionPoint;
import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder;
import com.powsybl.dynawaltz.models.utils.SideUtils;
import com.powsybl.iidm.network.HvdcConverterStation;
import com.powsybl.iidm.network.HvdcLine;
import com.powsybl.iidm.network.TwoSides;

Expand Down Expand Up @@ -62,4 +63,8 @@ protected List<VarConnection> getVarConnectionsWith(EquipmentConnectionPoint con
protected final VarConnection getSimpleVarConnectionWithBus(EquipmentConnectionPoint connected, TwoSides side) {
return new VarConnection(TERMINAL_PREFIX + side.getNum(), connected.getTerminalVarName(side));
}

public List<HvdcConverterStation<?>> getConnectedStations() {
return List.of(equipment.getConverterStation1(), equipment.getConverterStation2());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

import com.powsybl.commons.PowsyblException;
import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder;
import com.powsybl.iidm.network.HvdcConverterStation;
import com.powsybl.iidm.network.HvdcLine;
import com.powsybl.iidm.network.TwoSides;

import java.util.List;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
Expand Down Expand Up @@ -39,4 +42,9 @@ public String getSwitchOffSignalEventVarName(TwoSides side) {
}
return super.getSwitchOffSignalEventVarName(side);
}

@Override
public List<HvdcConverterStation<?>> getConnectedStations() {
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
return List.of(danglingSide.isDangling(TwoSides.ONE) ? equipment.getConverterStation2() : equipment.getConverterStation1());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
package com.powsybl.dynawaltz.models.hvdc;

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.HvdcConverterStation;
import com.powsybl.dynawaltz.models.macroconnections.MacroConnectionsAdder;
import com.powsybl.iidm.network.HvdcLine;
import com.powsybl.iidm.network.TwoSides;

import java.util.List;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
Expand Down Expand Up @@ -39,4 +42,9 @@ public String getSwitchOffSignalEventVarName(TwoSides side) {
}
return super.getSwitchOffSignalEventVarName(side);
}

@Override
public List<HvdcConverterStation<?>> getConnectedStations() {
return List.of(danglingSide.isDangling(TwoSides.ONE) ? equipment.getConverterStation2() : equipment.getConverterStation1());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,50 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.powsybl.commons.PowsyblException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
*/
public class ParametersSet {

private final Map<String, Parameter> parameters = new LinkedHashMap<>();

private final List<Reference> references = new ArrayList<>();
private final Map<String, Parameter> parameters;
private final List<Reference> references;
private final String id;

public ParametersSet(@JsonProperty("id") String id) {
this.id = id;
this.parameters = new LinkedHashMap<>();
this.references = new ArrayList<>();
}

public ParametersSet(String id, ParametersSet parametersSet) {
this.id = id;
this.parameters = new LinkedHashMap<>(parametersSet.parameters);
this.references = new ArrayList<>(parametersSet.references);
}

public void addParameter(String name, ParameterType type, String value) {
parameters.put(name, new Parameter(name, type, value));
}

public void addParameter(Parameter parameter) {
parameters.put(parameter.name(), parameter);
}

public void replaceParameter(String parameterName, ParameterType type, String value) {
parameters.replace(parameterName, new Parameter(parameterName, type, value));
}

public void addReference(String name, ParameterType type, String origData, String origName, String componentId) {
references.add(new Reference(name, type, origData, origName, componentId));
}

public void addReference(String name, ParameterType type, String origData, String origName) {
references.add(new Reference(name, type, origData, origName, null));
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -70,16 +86,15 @@ public String getString(String parameterName) {
return parameter.value();
}

public Parameter getParameter(String parameterName) {
public boolean hasParameter(String parameterName) {
return parameters.containsKey(parameterName);
}

private Parameter getParameter(String parameterName, ParameterType type) {
Parameter parameter = parameters.get(parameterName);
if (parameter == null) {
throw new IllegalArgumentException("Parameter " + parameterName + " not found in set " + id);
}
return parameter;
}

private Parameter getParameter(String parameterName, ParameterType type) {
Parameter parameter = getParameter(parameterName);
if (parameter.type() != type) {
throw new PowsyblException("Invalid parameter type: " + parameter.type() + " (" + type + " expected)");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.commons.PowsyblException;
import com.powsybl.dynawaltz.parameters.ParameterType;
import com.powsybl.dynawaltz.parameters.ParametersSet;
import com.powsybl.dynawaltz.xml.ParametersXml;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -57,6 +58,8 @@ void checkParameters() {
assertEquals(1, set2.getInt("generator_ExcitationPu"));

ParametersSet set3 = dParameters.getModelParameters("test");
assertFalse(set3.hasParameter("unknown"));
assertTrue(set3.hasParameter("boolean"));
assertTrue(set3.getBool("boolean"));
assertEquals("aString", set3.getString("string"));
}
Expand All @@ -74,4 +77,31 @@ 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"));
}

@Test
void replaceParameter() {
ParametersSet set = new ParametersSet("test");
String param = "modifiedParam";
set.addParameter(param, ParameterType.DOUBLE, "2.2");
set.replaceParameter(param, ParameterType.INT, "3");
assertEquals(3, set.getInt(param));
assertThrows(PowsyblException.class, () -> set.getDouble(param));
}

@Test
void copyParametersSet() {
ParametersSet set0 = new ParametersSet("test");
set0.addParameter("param", ParameterType.INT, "2");
ParametersSet set1 = new ParametersSet("copy", set0);
assertEquals(2, set1.getInt("param"));
}
}
Loading
Loading