Skip to content

Commit

Permalink
Fix merge loads parameter serialization
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Jun 25, 2024
1 parent 62865d6 commit 67086f4
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 42 deletions.
48 changes: 33 additions & 15 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ public boolean isSerializable() {
}

private static final String CHOSEN_OUTPUT_STRING_DELIMITER = ",";
protected static final String SVC_REGULATION_ON = "svcRegulationOn";
protected static final String SHUNT_REGULATION_ON = "shuntRegulationOn";
protected static final String AUTOMATIC_SLACK_BUS_ON = "automaticSlackBusOn";
protected static final String DSO_VOLTAGE_LEVEL = "dsoVoltageLevel";
protected static final String ACTIVE_POWER_COMPENSATION = "activePowerCompensation";
protected static final String SETTING_PATH = "settingPath";
protected static final String ASSEMBLING_PATH = "assemblingPath";
protected static final String START_TIME = "startTime";
protected static final String STOP_TIME = "stopTime";
protected static final String PRECISION_NAME = "precision";
protected static final String CHOSEN_OUTPUTS = "chosenOutputs";
protected static final String TIME_STEP = "timeStep";
protected static final String STARTING_POINT_MODE = "startingPointMode";
protected static final String MERGE_LOADS = "mergeLoads";
private static final String SVC_REGULATION_ON = "svcRegulationOn";
private static final String SHUNT_REGULATION_ON = "shuntRegulationOn";
private static final String AUTOMATIC_SLACK_BUS_ON = "automaticSlackBusOn";
private static final String DSO_VOLTAGE_LEVEL = "dsoVoltageLevel";
private static final String ACTIVE_POWER_COMPENSATION = "activePowerCompensation";
private static final String SETTING_PATH = "settingPath";
private static final String ASSEMBLING_PATH = "assemblingPath";
private static final String START_TIME = "startTime";
private static final String STOP_TIME = "stopTime";
private static final String PRECISION_NAME = "precision";
private static final String CHOSEN_OUTPUTS = "chosenOutputs";
private static final String TIME_STEP = "timeStep";
private static final String STARTING_POINT_MODE = "startingPointMode";
private static final String MERGE_LOADS = "mergeLoads";

private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> enumClass) {
return EnumSet.allOf(enumClass).stream().map(Enum::name).collect(Collectors.toList());
Expand Down Expand Up @@ -261,7 +261,6 @@ public DynaFlowParameters setSa(Sa securityAnalysis) {
return this;
}

@JsonIgnore
public boolean isMergeLoads() {
return mergeLoads;
}
Expand Down Expand Up @@ -358,6 +357,25 @@ public void update(Map<String, String> properties) {
Optional.ofNullable(properties.get(MERGE_LOADS)).ifPresent(prop -> setMergeLoads(Boolean.parseBoolean(prop)));
}

public Map<String, String> createMapFromParameters() {
return Map.ofEntries(
Map.entry(SVC_REGULATION_ON, Boolean.toString(getSvcRegulationOn())),
Map.entry(SHUNT_REGULATION_ON, Boolean.toString(getShuntRegulationOn())),
Map.entry(AUTOMATIC_SLACK_BUS_ON, Boolean.toString(getAutomaticSlackBusOn())),
Map.entry(DSO_VOLTAGE_LEVEL, Double.toString(getDsoVoltageLevel())),
Map.entry(ACTIVE_POWER_COMPENSATION, getActivePowerCompensation().name()),
Map.entry(SETTING_PATH, getSettingPath()),
Map.entry(ASSEMBLING_PATH, getAssemblingPath()),
Map.entry(START_TIME, Double.toString(getStartTime())),
Map.entry(STOP_TIME, Double.toString(getStopTime())),
Map.entry(PRECISION_NAME, Double.toString(getPrecision())),
Map.entry(Sa.TIME_OF_EVENT, Double.toString(getTimeOfEvent())),
Map.entry(CHOSEN_OUTPUTS, String.join(", ", getChosenOutputs())),
Map.entry(TIME_STEP, Double.toString(getTimeStep())),
Map.entry(STARTING_POINT_MODE, getStartingPointMode().name()),
Map.entry(MERGE_LOADS, Boolean.toString(isMergeLoads())));
}

public static DynaFlowParameters load(Map<String, String> properties) {
DynaFlowParameters parameters = new DynaFlowParameters();
parameters.update(properties);
Expand Down
21 changes: 4 additions & 17 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.concurrent.CompletableFuture;

import static com.powsybl.dynaflow.DynaFlowConstants.*;
import static com.powsybl.dynaflow.DynaFlowParameters.*;
import static com.powsybl.dynawo.commons.DynawoConstants.DYNAWO_TIMELINE_FOLDER;

/**
Expand Down Expand Up @@ -141,22 +140,10 @@ public Optional<Extension<LoadFlowParameters>> loadSpecificParameters(Map<String

@Override
public Map<String, String> createMapFromSpecificParameters(Extension<LoadFlowParameters> extension) {
return Map.ofEntries(
Map.entry(SVC_REGULATION_ON, Boolean.toString(((DynaFlowParameters) extension).getSvcRegulationOn())),
Map.entry(SHUNT_REGULATION_ON, Boolean.toString(((DynaFlowParameters) extension).getShuntRegulationOn())),
Map.entry(AUTOMATIC_SLACK_BUS_ON, Boolean.toString(((DynaFlowParameters) extension).getAutomaticSlackBusOn())),
Map.entry(DSO_VOLTAGE_LEVEL, Double.toString(((DynaFlowParameters) extension).getDsoVoltageLevel())),
Map.entry(ACTIVE_POWER_COMPENSATION, ((DynaFlowParameters) extension).getActivePowerCompensation().name()),
Map.entry(SETTING_PATH, ((DynaFlowParameters) extension).getSettingPath()),
Map.entry(ASSEMBLING_PATH, ((DynaFlowParameters) extension).getAssemblingPath()),
Map.entry(START_TIME, Double.toString(((DynaFlowParameters) extension).getStartTime())),
Map.entry(STOP_TIME, Double.toString(((DynaFlowParameters) extension).getStopTime())),
Map.entry(PRECISION_NAME, Double.toString(((DynaFlowParameters) extension).getPrecision())),
Map.entry(Sa.TIME_OF_EVENT, Double.toString(((DynaFlowParameters) extension).getSa().getTimeOfEvent())),
Map.entry(CHOSEN_OUTPUTS, String.join(", ", ((DynaFlowParameters) extension).getChosenOutputs())),
Map.entry(TIME_STEP, Double.toString(((DynaFlowParameters) extension).getTimeStep())),
Map.entry(STARTING_POINT_MODE, ((DynaFlowParameters) extension).getStartingPointMode().name()),
Map.entry(MERGE_LOADS, Boolean.toString(((DynaFlowParameters) extension).isMergeLoads())));
if (extension instanceof DynaFlowParameters dfp) {
dfp.createMapFromParameters();
}
return Collections.emptyMap();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import java.nio.file.Path;

/**
*
* Serializes parameters from {@link DynaFlowParameters} used by Dynawo.
* Some parameters are directly used by powsybl-dynawo and thus not serialized.
* @author Guillaume Pernin {@literal <guillaume.pernin at rte-france.com>}
*/
public final class DynaFlowConfigSerializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.io.IOException;

/**
*
* Represents {@link DynaFlowParameters} as a Json extension of {@link LoadFlowParameters}
* @author Guillaume Pernin {@literal <guillaume.pernin at rte-france.com>}
*/
@AutoService(ExtensionJsonSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,11 @@ void checkParameters() {
double startTime = 0.;
double stopTime = 100.;
double precision = 15.45;
double timeOfEvent = 10.;
double timeOfEvent = 14.;
List<String> chosenOutputs = Arrays.asList(OutputTypes.STEADYSTATE.name(), OutputTypes.TIMELINE.name());
double timeStep = 0;
StartingPointMode startingPointMode = StartingPointMode.FLAT;
boolean mergeLoads = false;

DynaFlowParameters.Sa securityAnalysis = new DynaFlowParameters.Sa();
securityAnalysis.setTimeOfEvent(2.);
boolean mergeLoads = true;

MapModuleConfig moduleConfig = platformConfig.createModuleConfig(MODULE_SPECIFIC_PARAMETERS);
moduleConfig.setStringProperty("svcRegulationOn", Boolean.toString(svcRegulationOn));
Expand Down Expand Up @@ -205,6 +202,7 @@ void defaultParametersSerialization() throws IOException {

try (InputStream actual = Files.newInputStream(parameterFile);
InputStream expected = getClass().getResourceAsStream("/params_default.json")) {
assertNotNull(expected);
compareTxt(expected, actual);
}
}
Expand All @@ -230,7 +228,7 @@ void parametersSerialization() throws IOException {
.setChosenOutputs(Collections.singletonList(OutputTypes.STEADYSTATE.name()))
.setTimeStep(2.6)
.setStartingPointMode(StartingPointMode.WARM)
.setMergeLoads(false);
.setMergeLoads(true);
lfParameters.addExtension(DynaFlowParameters.class, dynaFlowParameters);

Path workingDir = fileSystem.getPath("dynaflow/workingDir");
Expand All @@ -239,6 +237,7 @@ void parametersSerialization() throws IOException {

try (InputStream actual = Files.newInputStream(parameterFile);
InputStream expected = getClass().getResourceAsStream("/params.json")) {
assertNotNull(expected);
compareTxt(expected, actual);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void testDeserialize() {
assertArrayEquals(expectedChosenOutputs.toArray(), dynaFlowParameters.getChosenOutputs().toArray());
assertEquals(expectedTimeStep, dynaFlowParameters.getTimeStep(), 0.1d);
assertEquals(DynaFlowConstants.StartingPointMode.WARM, dynaFlowParameters.getStartingPointMode());
assertFalse(dynaFlowParameters.isMergeLoads());

assertTrue(lfParameters.isTransformerVoltageControlOn());
assertFalse(lfParameters.isPhaseShifterRegulationOn());
Expand Down Expand Up @@ -86,7 +87,8 @@ void roundTripParameters() throws IOException {
.setTimeOfEvent(10.)
.setChosenOutputs(Collections.singletonList(DynaFlowConstants.OutputTypes.STEADYSTATE.name()))
.setTimeStep(2.6)
.setStartingPointMode(DynaFlowConstants.StartingPointMode.WARM);
.setStartingPointMode(DynaFlowConstants.StartingPointMode.WARM)
.setMergeLoads(false);

parameters.addExtension(DynaFlowParameters.class, params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"dcPowerFactor" : 1.0,
"extensions" : {
"DynaFlowParameters" : {
"chosenOutputs" : [ "TIMELINE" ]
"chosenOutputs" : [ "TIMELINE" ],
"mergeLoads" : true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"chosenOutputs" : [ "STEADYSTATE" ],
"timeStep" : 2.6,
"startingPointMode" : "warm",
"mergeLoads" : false,
"sa" : {
"timeOfEvent" : 10.0
}
Expand Down

0 comments on commit 67086f4

Please sign in to comment.