Skip to content

Commit

Permalink
fix scaling generator when dupe (#19)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt authored Jan 27, 2025
1 parent 621f13f commit 7a3ae77
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

import com.powsybl.iidm.network.IdentifiableType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode
@Schema(description = "Identifiable attributes")
public class IdentifiableAttributes {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import static org.gridsuite.modification.utils.ModificationUtils.createReport;
import static org.gridsuite.modification.utils.ModificationUtils.distinctByKey;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void apply(Network network, ReportNode subReportNode) {

// apply variations
scalingInfos.getVariations().forEach(variation -> {
List<IdentifiableAttributes> identifiableAttributes = ModificationUtils.getIdentifiableAttributes(exportFilters, variation.getFilters(), subReportNode);
Set<IdentifiableAttributes> identifiableAttributes = ModificationUtils.getIdentifiableAttributes(exportFilters, variation.getFilters(), subReportNode);

if (CollectionUtils.isEmpty(identifiableAttributes)) {
String filterNames = variation.getFilters().stream().map(FilterInfos::getName).collect(Collectors.joining(", "));
Expand All @@ -75,7 +75,7 @@ public void apply(Network network, ReportNode subReportNode) {

private void applyVariation(Network network,
ReportNode subReportNode,
List<IdentifiableAttributes> identifiableAttributes,
Set<IdentifiableAttributes> identifiableAttributes,
ScalingVariationInfos variation) {
switch (variation.getVariationMode()) {
case PROPORTIONAL:
Expand All @@ -98,7 +98,7 @@ private void applyVariation(Network network,
}
}

private Double getDistributionKeys(List<IdentifiableAttributes> identifiableAttributes, ReportNode subReportNode) {
private Double getDistributionKeys(Set<IdentifiableAttributes> identifiableAttributes, ReportNode subReportNode) {
var distributionKeys = identifiableAttributes.stream()
.filter(equipment -> equipment.getDistributionKey() != null)
.mapToDouble(IdentifiableAttributes::getDistributionKey)
Expand All @@ -110,15 +110,15 @@ private Double getDistributionKeys(List<IdentifiableAttributes> identifiableAttr
return distributionKeys;
}

protected abstract void applyStackingUpVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);
protected abstract void applyStackingUpVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);

protected abstract void applyVentilationVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos, Double distributionKeys);
protected abstract void applyVentilationVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos, Double distributionKeys);

protected abstract void applyRegularDistributionVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);
protected abstract void applyRegularDistributionVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);

protected abstract void applyProportionalToPmaxVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);
protected abstract void applyProportionalToPmaxVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);

protected abstract void applyProportionalVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);
protected abstract void applyProportionalVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos);

protected abstract double getAsked(ScalingVariationInfos variationInfos, AtomicReference<Double> sum);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import static org.gridsuite.modification.utils.ModificationUtils.createReport;
import static org.gridsuite.modification.utils.ModificationUtils.distinctByKey;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -74,7 +71,7 @@ public void apply(Network network, ReportNode subReportNode) {
Map<UUID, FilterEquipments> exportFilters = ModificationUtils.getUuidFilterEquipmentsMap(filterService, network, subReportNode, filters, modificationInfos.getErrorType());
if (exportFilters != null) {
ModificationUtils.logWrongEquipmentsIdsFilters(subReportNode, exportFilters, filters);
List<IdentifiableAttributes> identifiableAttributes = ModificationUtils.getIdentifiableAttributes(exportFilters, modificationInfos.getFilters(), subReportNode);
Set<IdentifiableAttributes> identifiableAttributes = ModificationUtils.getIdentifiableAttributes(exportFilters, modificationInfos.getFilters(), subReportNode);

if (CollectionUtils.isEmpty(identifiableAttributes)) {
String filterNames = modificationInfos.getFilters().stream().map(FilterInfos::getName).collect(Collectors.joining(", "));
Expand All @@ -99,7 +96,7 @@ public String getName() {
return "ByFilterDeletion";
}

private void applyFilterDeletion(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes) {
private void applyFilterDeletion(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes) {
IdentifiableType identifiableType = modificationInfos.getEquipmentType();
if (CONNECTABLE_TYPES.contains(identifiableType)) {
identifiableAttributes.forEach(identifiableAttribute -> new RemoveFeederBay(identifiableAttribute.getId()).apply(network, true, subReportNode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import org.gridsuite.modification.dto.ScalingVariationInfos;
import org.gridsuite.modification.utils.ModificationUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

Expand All @@ -42,7 +38,7 @@ public GeneratorScaling(GeneratorScalingInfos generatorScalableInfos) {
@Override
protected void applyStackingUpVariation(Network network,
ReportNode subReportNode,
List<IdentifiableAttributes> identifiableAttributes,
Set<IdentifiableAttributes> identifiableAttributes,
ScalingVariationInfos generatorScalingVariation) {
AtomicReference<Double> sum = new AtomicReference<>(0D);
Scalable stackingUpScalable = Scalable.stack(identifiableAttributes.stream()
Expand All @@ -58,7 +54,7 @@ protected void applyStackingUpVariation(Network network,
@Override
protected void applyVentilationVariation(Network network,
ReportNode subReportNode,
List<IdentifiableAttributes> identifiableAttributes,
Set<IdentifiableAttributes> identifiableAttributes,
ScalingVariationInfos generatorScalingVariation,
Double distributionKeys) {
if (distributionKeys != null) {
Expand All @@ -82,7 +78,7 @@ protected void applyVentilationVariation(Network network,
@Override
protected void applyRegularDistributionVariation(Network network,
ReportNode subReportNode,
List<IdentifiableAttributes> identifiableAttributes,
Set<IdentifiableAttributes> identifiableAttributes,
ScalingVariationInfos generatorScalingVariation) {
List<Generator> generators = identifiableAttributes
.stream()
Expand All @@ -106,7 +102,7 @@ protected void applyRegularDistributionVariation(Network network,
@Override
protected void applyProportionalToPmaxVariation(Network network,
ReportNode subReportNode,
List<IdentifiableAttributes> identifiableAttributes,
Set<IdentifiableAttributes> identifiableAttributes,
ScalingVariationInfos generatorScalingVariation) {
AtomicReference<Double> maxPSum = new AtomicReference<>(0D);
AtomicReference<Double> targetPSum = new AtomicReference<>(0D);
Expand Down Expand Up @@ -135,7 +131,7 @@ protected void applyProportionalToPmaxVariation(Network network,
@Override
protected void applyProportionalVariation(Network network,
ReportNode subReportNode,
List<IdentifiableAttributes> identifiableAttributes,
Set<IdentifiableAttributes> identifiableAttributes,
ScalingVariationInfos generatorScalingVariation) {
AtomicReference<Double> sum = new AtomicReference<>(0D);
List<Generator> generators = identifiableAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
import org.gridsuite.modification.dto.ScalingVariationInfos;
import org.gridsuite.modification.utils.ModificationUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

Expand All @@ -38,7 +34,7 @@ public LoadScaling(LoadScalingInfos loadScalableInfos) {
}

@Override
protected void applyVentilationVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos, Double distributionKeys) {
protected void applyVentilationVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos, Double distributionKeys) {
if (distributionKeys != null) {
AtomicReference<Double> sum = new AtomicReference<>(0D);
List<Double> percentages = new ArrayList<>();
Expand All @@ -60,7 +56,7 @@ protected void applyVentilationVariation(Network network, ReportNode subReportNo
}

@Override
protected void applyRegularDistributionVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
protected void applyRegularDistributionVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
List<Load> loads = identifiableAttributes
.stream()
.map(attribute -> network.getLoad(attribute.getId()))
Expand All @@ -83,7 +79,7 @@ protected void applyRegularDistributionVariation(Network network, ReportNode sub
}

@Override
protected void applyProportionalVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
protected void applyProportionalVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
List<Load> loads = identifiableAttributes.stream()
.map(attribute -> network.getLoad(attribute.getId()))
.filter(ModificationUtils::isInjectionConnected)
Expand All @@ -108,13 +104,13 @@ protected void applyProportionalVariation(Network network, ReportNode subReportN
}

@Override
protected void applyProportionalToPmaxVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
protected void applyProportionalToPmaxVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
// no implementation for load scaling
throw new NetworkModificationException(scalingInfos.getErrorType(), String.format("This variation mode is not supported : %s", scalingVariationInfos.getVariationMode().name()));
}

@Override
protected void applyStackingUpVariation(Network network, ReportNode subReportNode, List<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
protected void applyStackingUpVariation(Network network, ReportNode subReportNode, Set<IdentifiableAttributes> identifiableAttributes, ScalingVariationInfos scalingVariationInfos) {
// no implementation for load scaling
throw new NetworkModificationException(scalingInfos.getErrorType(), String.format("This variation mode is not supported : %s", scalingVariationInfos.getVariationMode().name()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -1506,7 +1507,7 @@ public boolean isValidFilter(ReportNode subReportNode,
return true;
}

public static List<IdentifiableAttributes> getIdentifiableAttributes(Map<UUID, FilterEquipments> exportFilters, List<FilterInfos> filterInfos, ReportNode subReportNode) {
public static Set<IdentifiableAttributes> getIdentifiableAttributes(Map<UUID, FilterEquipments> exportFilters, List<FilterInfos> filterInfos, ReportNode subReportNode) {
filterInfos.stream()
.filter(f -> !exportFilters.containsKey(f.getId()))
.forEach(f -> createReport(subReportNode,
Expand All @@ -1520,7 +1521,7 @@ public static List<IdentifiableAttributes> getIdentifiableAttributes(Map<UUID, F
.flatMap(f -> exportFilters.get(f.getId())
.getIdentifiableAttributes()
.stream())
.toList();
.collect(Collectors.toCollection(LinkedHashSet::new));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ void specificSetUp() {
}

private static Map<UUID, FilterEquipments> getTestFilters() {
FilterEquipments filter1 = FilterEquipments.builder().filterId(FILTER_ID_1).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_1, IdentifiableType.LOAD, 1.0),
new IdentifiableAttributes(GENERATOR_ID_2, IdentifiableType.LOAD, 2.0))).build();
FilterEquipments filter1 = FilterEquipments.builder().filterId(FILTER_ID_1).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_1, IdentifiableType.GENERATOR, 1.0),
new IdentifiableAttributes(GENERATOR_ID_2, IdentifiableType.GENERATOR, 2.0))).build();

FilterEquipments filter2 = FilterEquipments.builder().filterId(FILTER_ID_2).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_3, IdentifiableType.LOAD, 2.0),
new IdentifiableAttributes(GENERATOR_ID_4, IdentifiableType.LOAD, 5.0))).build();
FilterEquipments filter2 = FilterEquipments.builder().filterId(FILTER_ID_2).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_3, IdentifiableType.GENERATOR, 2.0),
new IdentifiableAttributes(GENERATOR_ID_4, IdentifiableType.GENERATOR, 5.0))).build();

FilterEquipments filter3 = FilterEquipments.builder().filterId(FILTER_ID_3).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_5, IdentifiableType.LOAD, 6.0),
new IdentifiableAttributes(GENERATOR_ID_6, IdentifiableType.LOAD, 7.0))).build();
FilterEquipments filter3 = FilterEquipments.builder().filterId(FILTER_ID_3).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_5, IdentifiableType.GENERATOR, 6.0),
new IdentifiableAttributes(GENERATOR_ID_6, IdentifiableType.GENERATOR, 7.0))).build();

FilterEquipments filter4 = FilterEquipments.builder().filterId(FILTER_ID_4).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_7, IdentifiableType.LOAD, 3.0),
new IdentifiableAttributes(GENERATOR_ID_8, IdentifiableType.LOAD, 8.0))).build();
FilterEquipments filter4 = FilterEquipments.builder().filterId(FILTER_ID_4).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_7, IdentifiableType.GENERATOR, 3.0),
new IdentifiableAttributes(GENERATOR_ID_8, IdentifiableType.GENERATOR, 8.0))).build();

FilterEquipments filter5 = FilterEquipments.builder().filterId(FILTER_ID_5).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_9, IdentifiableType.LOAD, 0.0),
new IdentifiableAttributes(GENERATOR_ID_10, IdentifiableType.LOAD, 9.0))).build();
FilterEquipments filter5 = FilterEquipments.builder().filterId(FILTER_ID_5).identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_9, IdentifiableType.GENERATOR, 0.0),
new IdentifiableAttributes(GENERATOR_ID_10, IdentifiableType.GENERATOR, 9.0))).build();

return Map.of(FILTER_ID_1, filter1, FILTER_ID_2, filter2, FILTER_ID_3, filter3, FILTER_ID_4, filter4, FILTER_ID_5, filter5);
}
Expand All @@ -106,9 +106,9 @@ private static Map<UUID, FilterEquipments> getTestFilters() {
public void testApply() throws Exception {
GeneratorScalingInfos modificationInfo = (GeneratorScalingInfos) buildModification();
when(filterService.getUuidFilterEquipmentsMap(any(), any())).thenReturn(getTestFilters());
GeneratorScaling loadScaling = (GeneratorScaling) modificationInfo.toModification();
loadScaling.initApplicationContext(filterService);
loadScaling.apply(getNetwork());
GeneratorScaling generatorScaling = (GeneratorScaling) modificationInfo.toModification();
generatorScaling.initApplicationContext(filterService);
generatorScaling.apply(getNetwork());
assertAfterNetworkModificationApplication();
}

Expand Down Expand Up @@ -179,7 +179,7 @@ void testScalingCreationWithWarning() throws Exception {
Map<UUID, FilterEquipments> filters = Map.of(FILTER_ID_5, FilterEquipments.builder()
.filterId(FILTER_ID_5)
.identifiableAttributes(List.of(new IdentifiableAttributes(GENERATOR_ID_9, IdentifiableType.GENERATOR, 0.0),
new IdentifiableAttributes(GENERATOR_ID_10, IdentifiableType.LOAD, 9.0)))
new IdentifiableAttributes(GENERATOR_ID_10, IdentifiableType.GENERATOR, 9.0)))
.build(),
FILTER_WRONG_ID_2, FilterEquipments.builder()
.filterId(FILTER_WRONG_ID_2)
Expand Down

0 comments on commit 7a3ae77

Please sign in to comment.