Skip to content

Commit

Permalink
Delete DynaFlowParameters.Sa inner class and replace it with a proper…
Browse files Browse the repository at this point in the history
… DynaFlowSecurityAnalysisParameters class extension of SecurityAnalysisParameter

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Oct 14, 2024
1 parent 78f0d22 commit 4d2ef34
Show file tree
Hide file tree
Showing 23 changed files with 618 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package com.powsybl.dynaflow;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.common.base.MoreObjects;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
Expand All @@ -24,7 +22,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
Expand All @@ -38,45 +35,6 @@
public class DynaFlowParameters extends AbstractExtension<LoadFlowParameters> {

private static final Logger LOGGER = LoggerFactory.getLogger(DynaFlowParameters.class);

/**
* Inner class dedicated to Security Analysis (SA) namespace
*/
public static class Sa {
private static final String SECURITY_ANALYSIS = "sa"; //Security analysis
private static final double DEFAULT_TIME_OF_EVENT = 10d;
protected static final String TIME_OF_EVENT = "timeOfEvent";

private Double timeOfEvent = DEFAULT_TIME_OF_EVENT;

public Double getTimeOfEvent() {
return timeOfEvent;
}

public void setTimeOfEvent(Double timeOfEvent) {
this.timeOfEvent = timeOfEvent;
}

@Override
public String toString() {
return MoreObjects.toStringHelper("").omitNullValues()
.add(TIME_OF_EVENT, timeOfEvent).toString();
}

public static void writeJson(JsonGenerator jsonGenerator, DynaFlowParameters dynaFlowParameters) throws IOException {
if (dynaFlowParameters.getSa().isSerializable()) {
jsonGenerator.writeObjectFieldStart("sa");
jsonGenerator.writeNumberField("TimeOfEvent", dynaFlowParameters.getTimeOfEvent());
jsonGenerator.writeEndObject();
}
}

@JsonIgnore
public boolean isSerializable() {
return timeOfEvent != null;
}
}

private static final String CHOSEN_OUTPUT_STRING_DELIMITER = ",";
private static final String SVC_REGULATION_ON = "svcRegulationOn";
private static final String SHUNT_REGULATION_ON = "shuntRegulationOn";
Expand Down Expand Up @@ -122,7 +80,6 @@ private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> e
new Parameter(START_TIME, ParameterType.DOUBLE, "Start time", DEFAULT_START_TIME),
new Parameter(STOP_TIME, ParameterType.DOUBLE, "Stop time", DEFAULT_STOP_TIME),
new Parameter(PRECISION_NAME, ParameterType.DOUBLE, "Precision", DEFAULT_PRECISION),
new Parameter(Sa.TIME_OF_EVENT, ParameterType.DOUBLE, "Time of event", Sa.DEFAULT_TIME_OF_EVENT),
new Parameter(CHOSEN_OUTPUTS, ParameterType.STRING_LIST, "Chosen outputs", DEFAULT_CHOSEN_OUTPUTS.stream().map(OutputTypes::name).toList(), getEnumPossibleValues(OutputTypes.class), ParameterScope.TECHNICAL),
new Parameter(TIME_STEP, ParameterType.DOUBLE, "Time step", DEFAULT_TIME_STEP),
new Parameter(STARTING_POINT_MODE, ParameterType.STRING, "Starting point mode", DEFAULT_STARTING_POINT_MODE.name(), getEnumPossibleValues(StartingPointMode.class)),
Expand All @@ -138,7 +95,6 @@ private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> e
private double startTime = DEFAULT_START_TIME;
private double stopTime = DEFAULT_STOP_TIME;
private Double precision = null;
private Sa securityAnalysis = null;
private EnumSet<OutputTypes> chosenOutputs = DEFAULT_CHOSEN_OUTPUTS;
private double timeStep = DEFAULT_TIME_STEP;
private StartingPointMode startingPointMode = DEFAULT_STARTING_POINT_MODE;
Expand Down Expand Up @@ -234,19 +190,6 @@ public DynaFlowParameters setPrecision(Double precision) {
return this;
}

@JsonIgnore
public Double getTimeOfEvent() {
return securityAnalysis == null ? null : securityAnalysis.getTimeOfEvent();
}

public DynaFlowParameters setTimeOfEvent(Double timeOfEvent) {
if (this.securityAnalysis == null) {
securityAnalysis = new Sa();
}
securityAnalysis.setTimeOfEvent(timeOfEvent);
return this;
}

public Set<OutputTypes> getChosenOutputs() {
return chosenOutputs;
}
Expand Down Expand Up @@ -279,15 +222,6 @@ public DynaFlowParameters setStartingPointMode(StartingPointMode startingPointMo
return this;
}

public Sa getSa() {
return this.securityAnalysis;
}

public DynaFlowParameters setSa(Sa securityAnalysis) {
this.securityAnalysis = securityAnalysis;
return this;
}

public boolean isMergeLoads() {
return mergeLoads;
}
Expand Down Expand Up @@ -315,7 +249,6 @@ public String toString() {
.add(START_TIME, startTime)
.add(STOP_TIME, stopTime)
.add(PRECISION_NAME, precision)
.add(Sa.SECURITY_ANALYSIS, securityAnalysis)
.add(CHOSEN_OUTPUTS, chosenOutputs)
.add(TIME_STEP, timeStep)
.add(STARTING_POINT_MODE, startingPointMode)
Expand All @@ -326,10 +259,8 @@ public String toString() {
public static DynaFlowParameters load(PlatformConfig platformConfig) {
Objects.requireNonNull(platformConfig);
DynaFlowParameters parameters = new DynaFlowParameters();

platformConfig.getOptionalModuleConfig(MODULE_SPECIFIC_PARAMETERS)
.ifPresent(config -> load(parameters, config));

return parameters;
}

Expand All @@ -341,6 +272,12 @@ public static DynaFlowParameters load(ModuleConfig config) {
return parameters;
}

public static DynaFlowParameters load(Map<String, String> properties) {
DynaFlowParameters parameters = new DynaFlowParameters();
parameters.update(properties);
return parameters;
}

private static void load(DynaFlowParameters parameters, ModuleConfig config) {
config.getOptionalBooleanProperty(SVC_REGULATION_ON).ifPresent(parameters::setSvcRegulationOn);
config.getOptionalBooleanProperty(SHUNT_REGULATION_ON).ifPresent(parameters::setShuntRegulationOn);
Expand All @@ -352,7 +289,6 @@ private static void load(DynaFlowParameters parameters, ModuleConfig config) {
config.getOptionalDoubleProperty(START_TIME).ifPresent(parameters::setStartTime);
config.getOptionalDoubleProperty(STOP_TIME).ifPresent(parameters::setStopTime);
config.getOptionalDoubleProperty(PRECISION_NAME).ifPresent(parameters::setPrecision);
config.getOptionalDoubleProperty(Sa.TIME_OF_EVENT).ifPresent(parameters::setTimeOfEvent);
config.getOptionalEnumSetProperty(CHOSEN_OUTPUTS, OutputTypes.class).ifPresent(parameters::setChosenOutputs);
config.getOptionalDoubleProperty(TIME_STEP).ifPresent(parameters::setTimeStep);
config.getOptionalStringProperty(STARTING_POINT_MODE).map(StartingPointMode::fromString).ifPresent(parameters::setStartingPointMode);
Expand Down Expand Up @@ -390,12 +326,6 @@ public void update(Map<String, String> properties) {
Optional.ofNullable(properties.get(START_TIME)).ifPresent(prop -> setStartTime(Double.parseDouble(prop)));
Optional.ofNullable(properties.get(STOP_TIME)).ifPresent(prop -> setStopTime(Double.parseDouble(prop)));
Optional.ofNullable(properties.get(PRECISION_NAME)).ifPresent(prop -> setPrecision(Double.parseDouble(prop)));
Optional.ofNullable(properties.get(Sa.TIME_OF_EVENT)).ifPresent(prop -> {
if (securityAnalysis == null) {
securityAnalysis = new Sa();
}
securityAnalysis.setTimeOfEvent(Double.parseDouble(prop));
});
Optional.ofNullable(properties.get(CHOSEN_OUTPUTS)).ifPresent(prop ->
setChosenOutputs(Stream.of(prop.split(CHOSEN_OUTPUT_STRING_DELIMITER)).map(o -> OutputTypes.valueOf(o.trim())).collect(Collectors.toSet())));
Optional.ofNullable(properties.get(TIME_STEP)).ifPresent(prop -> setTimeStep(Double.parseDouble(prop)));
Expand All @@ -417,7 +347,6 @@ public Map<String, String> createMapFromParameters() {
addNotNullEntry(START_TIME, startTime, parameters::put);
addNotNullEntry(STOP_TIME, stopTime, parameters::put);
addNotNullEntry(PRECISION_NAME, precision, parameters::put);
addNotNullEntry(Sa.TIME_OF_EVENT, getTimeOfEvent(), parameters::put);
if (!chosenOutputs.isEmpty()) {
parameters.put(CHOSEN_OUTPUTS, String.join(CHOSEN_OUTPUT_STRING_DELIMITER, chosenOutputs.stream().map(OutputTypes::name).toList()));
}
Expand All @@ -432,10 +361,4 @@ private void addNotNullEntry(String key, Object value, BiConsumer<String, String
adder.accept(key, Objects.toString(value));
}
}

public static DynaFlowParameters load(Map<String, String> properties) {
DynaFlowParameters parameters = new DynaFlowParameters();
parameters.update(properties);
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public static Command getVersionCommand(DynaFlowConfig config) {
.build();
}

private static DynaFlowParameters getParametersExt(LoadFlowParameters parameters) {
static DynaFlowParameters getParametersExt(LoadFlowParameters parameters) {
DynaFlowParameters parametersExt = parameters.getExtension(DynaFlowParameters.class);
if (parametersExt == null) {
parametersExt = new DynaFlowParameters();
return new DynaFlowParameters();
}
return parametersExt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,9 @@ private static void writeContingencies(List<Contingency> contingencies, Path wor
private static void writeParameters(SecurityAnalysisParameters securityAnalysisParameters, Path workingDir) throws IOException {
// TODO(Luma) Take into account also Security Analysis parameters
LoadFlowParameters loadFlowParameters = securityAnalysisParameters.getLoadFlowParameters();
DynaFlowParameters dynaFlowParameters = getParametersExt(loadFlowParameters);
DynaFlowConfigSerializer.serialize(loadFlowParameters, dynaFlowParameters, Path.of("."), workingDir.resolve(CONFIG_FILENAME));
}

private static DynaFlowParameters getParametersExt(LoadFlowParameters parameters) {
DynaFlowParameters parametersExt = parameters.getExtension(DynaFlowParameters.class);
if (parametersExt == null) {
parametersExt = new DynaFlowParameters();
}
return parametersExt;
DynaFlowParameters dynaFlowParameters = DynaFlowProvider.getParametersExt(loadFlowParameters);
DynaFlowSecurityAnalysisParameters dynaFlowSecurityAnalysisParameters = DynaFlowSecurityAnalysisProvider.getParametersExt(securityAnalysisParameters);
DynaFlowConfigSerializer.serialize(loadFlowParameters, dynaFlowParameters, dynaFlowSecurityAnalysisParameters,
Path.of("."), workingDir.resolve(CONFIG_FILENAME));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) 2024, 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.dynaflow;

import com.google.common.base.MoreObjects;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.security.SecurityAnalysisParameters;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static com.powsybl.dynaflow.DynaFlowSecurityAnalysisProvider.MODULE_SPECIFIC_PARAMETERS;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynaFlowSecurityAnalysisParameters extends AbstractExtension<SecurityAnalysisParameters> {

private static final double DEFAULT_TIME_OF_EVENT = 10d;
private static final String TIME_OF_EVENT = "timeOfEvent";
public static final List<String> SPECIFIC_PARAMETER_NAMES = List.of(TIME_OF_EVENT);

private Double timeOfEvent = DEFAULT_TIME_OF_EVENT;

public Double getTimeOfEvent() {
return timeOfEvent;
}

public DynaFlowSecurityAnalysisParameters setTimeOfEvent(Double timeOfEvent) {
this.timeOfEvent = timeOfEvent;
return this;
}

@Override
public String getName() {
return "DynaFlowSecurityAnalysisParameters";
}

@Override
public String toString() {
return MoreObjects.toStringHelper("").omitNullValues()
.add(TIME_OF_EVENT, timeOfEvent).toString();
}

public static DynaFlowSecurityAnalysisParameters load(PlatformConfig platformConfig) {
Objects.requireNonNull(platformConfig);
DynaFlowSecurityAnalysisParameters parameters = new DynaFlowSecurityAnalysisParameters();
platformConfig.getOptionalModuleConfig(MODULE_SPECIFIC_PARAMETERS)
.ifPresent(config -> load(parameters, config));
return parameters;
}

public static DynaFlowSecurityAnalysisParameters load(ModuleConfig config) {
DynaFlowSecurityAnalysisParameters parameters = new DynaFlowSecurityAnalysisParameters();
if (config != null) {
load(parameters, config);
}
return parameters;
}

public static DynaFlowSecurityAnalysisParameters load(Map<String, String> properties) {
DynaFlowSecurityAnalysisParameters parameters = new DynaFlowSecurityAnalysisParameters();
parameters.update(properties);
return parameters;
}

private static void load(DynaFlowSecurityAnalysisParameters parameters, ModuleConfig config) {
config.getOptionalDoubleProperty(TIME_OF_EVENT).ifPresent(parameters::setTimeOfEvent);
}

public void update(Map<String, String> properties) {
Objects.requireNonNull(properties);
Optional.ofNullable(properties.get(TIME_OF_EVENT)).ifPresent(prop -> setTimeOfEvent(Double.parseDouble(prop)));
}
}
Loading

0 comments on commit 4d2ef34

Please sign in to comment.