Skip to content

Commit

Permalink
Merge branch 'main' into add_modification_count_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Meklo authored Nov 21, 2023
2 parents 2476d01 + 901b28e commit 088cc84
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.ShuntCompensator;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -19,6 +20,7 @@
import org.gridsuite.modification.server.NetworkModificationException;
import org.gridsuite.modification.server.dto.formula.equipmentfield.BatteryField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.GeneratorField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField;

/**
* @author Seddik Yengui <Seddik.yengui at rte-france.com>
Expand Down Expand Up @@ -48,6 +50,7 @@ public Double getRefOrValue(Identifiable<?> identifiable) {
Double referenceValue = switch (identifiableType) {
case GENERATOR -> GeneratorField.getReferenceValue((Generator) identifiable, equipmentField);
case BATTERY -> BatteryField.getReferenceValue((Battery) identifiable, equipmentField);
case SHUNT_COMPENSATOR -> ShuntCompensatorField.getReferenceValue((ShuntCompensator) identifiable, equipmentField);
default -> throw new NetworkModificationException(NetworkModificationException.Type.BY_FORMULA_MODIFICATION_ERROR,
String.format("Unsupported equipment type : %s", identifiableType.name()));
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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/.
*/

package org.gridsuite.modification.server.dto.formula.equipmentfield;

import com.powsybl.iidm.network.ShuntCompensator;
import com.powsybl.iidm.network.ShuntCompensatorLinearModel;
import com.powsybl.iidm.network.ShuntCompensatorModelType;
import com.powsybl.iidm.network.VoltageLevel;
import org.gridsuite.modification.server.NetworkModificationException;

/**
* @author Seddik Yengui <Seddik.yengui at rte-france.com>
*/

public enum ShuntCompensatorField {
MAXIMUM_SECTION_COUNT,
SECTION_COUNT,
MAXIMUM_SUSCEPTANCE,
MAXIMUM_Q_AT_NOMINAL_VOLTAGE;

public static Double getReferenceValue(ShuntCompensator shuntCompensator, String shuntCompensatorField) {
VoltageLevel voltageLevel = shuntCompensator.getTerminal().getVoltageLevel();
ShuntCompensatorField field = ShuntCompensatorField.valueOf(shuntCompensatorField);
return switch (field) {
case MAXIMUM_SECTION_COUNT -> (double) shuntCompensator.getMaximumSectionCount();
case SECTION_COUNT -> (double) shuntCompensator.getSectionCount();
case MAXIMUM_SUSCEPTANCE -> shuntCompensator.getB() * shuntCompensator.getMaximumSectionCount();
case MAXIMUM_Q_AT_NOMINAL_VOLTAGE -> Math.abs(Math.pow(voltageLevel.getNominalV(), 2) * shuntCompensator.getB()) * shuntCompensator.getMaximumSectionCount();
};
}

public static void setNewValue(ShuntCompensator shuntCompensator, String shuntCompensatorField, Double newValue) {
if (shuntCompensator.getModelType() != ShuntCompensatorModelType.LINEAR) {
throw new NetworkModificationException(NetworkModificationException.Type.BY_FORMULA_MODIFICATION_ERROR,
String.format("Shunt compensator with %s model is not supported", shuntCompensator.getModelType()));
}
ShuntCompensatorLinearModel model = shuntCompensator.getModel(ShuntCompensatorLinearModel.class);
ShuntCompensatorField field = ShuntCompensatorField.valueOf(shuntCompensatorField);
VoltageLevel voltageLevel = shuntCompensator.getTerminal().getVoltageLevel();
switch (field) {
case MAXIMUM_SECTION_COUNT -> {
int maximumSectionCount = newValue.intValue();
model.setBPerSection(model.getBPerSection() * shuntCompensator.getMaximumSectionCount() / maximumSectionCount);
model.setMaximumSectionCount(maximumSectionCount);
}
case SECTION_COUNT -> shuntCompensator.setSectionCount(newValue.intValue());
case MAXIMUM_SUSCEPTANCE -> model.setBPerSection(newValue / shuntCompensator.getMaximumSectionCount());
case MAXIMUM_Q_AT_NOMINAL_VOLTAGE -> {
double newQatNominalV = newValue / shuntCompensator.getMaximumSectionCount();
double newSusceptancePerSection = newQatNominalV / Math.pow(voltageLevel.getNominalV(), 2);
model.setBPerSection(newSusceptancePerSection);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* 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/.
*/

package org.gridsuite.modification.server.modifications;

import com.powsybl.commons.reporter.Report;
Expand All @@ -7,6 +14,7 @@
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.ShuntCompensator;
import org.gridsuite.modification.server.NetworkModificationException;
import org.gridsuite.modification.server.dto.ByFormulaModificationInfos;
import org.gridsuite.modification.server.dto.FilterEquipments;
Expand All @@ -15,6 +23,7 @@
import org.gridsuite.modification.server.dto.formula.Operator;
import org.gridsuite.modification.server.dto.formula.equipmentfield.BatteryField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.GeneratorField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField;
import org.gridsuite.modification.server.service.FilterService;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -143,12 +152,9 @@ private void applyFormula(Network network,
Double value2 = formulaInfos.getFieldOrValue2().getRefOrValue(identifiable);
final Double newValue = applyOperation(formulaInfos.getOperator(), value1, value2);
switch (identifiable.getType()) {
case GENERATOR -> GeneratorField.setNewValue((Generator) identifiable,
formulaInfos.getEditedField(),
newValue);
case BATTERY -> BatteryField.setNewValue((Battery) identifiable,
formulaInfos.getEditedField(),
newValue);
case GENERATOR -> GeneratorField.setNewValue((Generator) identifiable, formulaInfos.getEditedField(), newValue);
case BATTERY -> BatteryField.setNewValue((Battery) identifiable, formulaInfos.getEditedField(), newValue);
case SHUNT_COMPENSATOR -> ShuntCompensatorField.setNewValue((ShuntCompensator) identifiable, formulaInfos.getEditedField(), newValue);
default -> throw new NetworkModificationException(NetworkModificationException.Type.BY_FORMULA_MODIFICATION_ERROR, "Unsupported equipment");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,67 @@

@Tag("IntegrationTest")
public abstract class AbstractByFormulaModificationTest extends AbstractNetworkModificationTest {
protected static final UUID FILTER_ID_1 = UUID.randomUUID();
protected static final UUID FILTER_ID_2 = UUID.randomUUID();
protected static final UUID FILTER_ID_3 = UUID.randomUUID();
protected static final UUID FILTER_ID_4 = UUID.randomUUID();
protected static final UUID FILTER_ID_5 = UUID.randomUUID();
protected static final UUID FILTER_WITH_ALL_WRONG_IDS = UUID.randomUUID();
protected static final UUID FILTER_WITH_ONE_WRONG_ID = UUID.randomUUID();
protected final FilterInfos filter1 = new FilterInfos(FILTER_ID_1, "filter1");
protected final FilterInfos filter2 = new FilterInfos(FILTER_ID_2, "filter2");
protected final FilterInfos filter3 = new FilterInfos(FILTER_ID_3, "filter3");
protected final FilterInfos filter4 = new FilterInfos(FILTER_ID_4, "filter4");
protected final FilterInfos filter5 = new FilterInfos(FILTER_ID_5, "filter5");
protected final FilterInfos filterWithAllWrongId = new FilterInfos(FILTER_WITH_ALL_WRONG_IDS, "filterWithAllWrongId");
protected final FilterInfos filterWithOneWrongId = new FilterInfos(FILTER_WITH_ONE_WRONG_ID, "filterWithOneWrongId");

public static final String PATH = "/v1/filters/export";

@Before
public void specificSetUp() {
FilterService.setFilterServerBaseUri(wireMockServer.baseUrl());

getNetwork().getVariantManager().setWorkingVariant("variant_1");
createEquipments();
}

protected void checkCreateWithWarning(List<FormulaInfos> formulaInfos, List<IdentifiableAttributes> existingEquipmentList) throws Exception {
FilterEquipments filter = getFilterEquipments(FILTER_WITH_ONE_WRONG_ID, "filterWithWrongId", existingEquipmentList, List.of("wrongId"));

UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + FILTER_WITH_ONE_WRONG_ID))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(List.of(filter)))
.withHeader("Content-Type", "application/json"))).getId();

ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder()
.formulaInfosList(formulaInfos)
.identifiableType(getIdentifiableType())
.build();

checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_WARNINGS);

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(FILTER_WITH_ONE_WRONG_ID)), false);
}

protected void checkCreateWithError(List<FormulaInfos> formulaInfos) throws Exception {
FilterEquipments filter = getFilterEquipments(FILTER_WITH_ALL_WRONG_IDS, "filterWithWrongId", List.of(), List.of("wrongId1", "wrongId2"));

UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + FILTER_WITH_ALL_WRONG_IDS))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(List.of(filter)))
.withHeader("Content-Type", "application/json"))).getId();

ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder()
.formulaInfosList(formulaInfos)
.identifiableType(getIdentifiableType())
.build();

checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_ERRORS);

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(FILTER_WITH_ALL_WRONG_IDS)), false);
}

@Test
@Override
public void testCreate() throws Exception {
Expand All @@ -66,7 +118,6 @@ public void testCreate() throws Exception {
super.testCreate();

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), filters.stream().map(FilterEquipments::getFilterId).collect(Collectors.toList())), false);

}

@Test
Expand All @@ -84,7 +135,7 @@ public void testCopy() throws Exception {
wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), filters.stream().map(FilterEquipments::getFilterId).collect(Collectors.toList())), false);
}

void checkCreationApplicationStatus(ByFormulaModificationInfos byFormulaModificationInfos,
private void checkCreationApplicationStatus(ByFormulaModificationInfos byFormulaModificationInfos,
NetworkModificationResult.ApplicationStatus applicationStatus) throws Exception {
String modificationToCreateJson = mapper.writeValueAsString(byFormulaModificationInfos);

Expand All @@ -104,7 +155,7 @@ protected Network createNetwork(UUID networkUuid) {
@Override
protected ByFormulaModificationInfos buildModification() {
return ByFormulaModificationInfos.builder()
.identifiableType(IdentifiableType.GENERATOR)
.identifiableType(getIdentifiableType())
.formulaInfosList(getFormulaInfos())
.stashed(false)
.build();
Expand All @@ -113,21 +164,21 @@ protected ByFormulaModificationInfos buildModification() {
@Override
protected ByFormulaModificationInfos buildModificationUpdate() {
return ByFormulaModificationInfos.builder()
.identifiableType(IdentifiableType.GENERATOR)
.identifiableType(getIdentifiableType())
.formulaInfosList(getUpdatedFormulaInfos())
.stashed(false)
.build();
}

IdentifiableAttributes getIdentifiableAttributes(String id, Double distributionKey) {
protected IdentifiableAttributes getIdentifiableAttributes(String id, Double distributionKey) {
return IdentifiableAttributes.builder()
.id(id)
.type(IdentifiableType.GENERATOR)
.type(getIdentifiableType())
.distributionKey(distributionKey)
.build();
}

FilterEquipments getFilterEquipments(UUID filterID, String filterName, List<IdentifiableAttributes> identifiableAttributes, List<String> notFoundEquipments) {
protected FilterEquipments getFilterEquipments(UUID filterID, String filterName, List<IdentifiableAttributes> identifiableAttributes, List<String> notFoundEquipments) {
return FilterEquipments.builder()
.filterId(filterID)
.filterName(filterName)
Expand All @@ -136,7 +187,7 @@ FilterEquipments getFilterEquipments(UUID filterID, String filterName, List<Iden
.build();
}

FormulaInfos getFormulaInfo(String editedField,
protected FormulaInfos getFormulaInfo(String editedField,
List<FilterInfos> filters,
Operator operator,
ReferenceFieldOrValue fieldOrValue1,
Expand All @@ -150,7 +201,7 @@ FormulaInfos getFormulaInfo(String editedField,
.build();
}

Map<String, StringValuePattern> handleQueryParams(UUID networkUuid, List<UUID> filterIds) {
private Map<String, StringValuePattern> handleQueryParams(UUID networkUuid, List<UUID> filterIds) {
return Map.of("networkUuid", WireMock.equalTo(String.valueOf(networkUuid)), "variantId", WireMock.equalTo("variant_1"), "ids", WireMock.matching(filterIds.stream().map(uuid -> ".+").collect(Collectors.joining(","))));
}

Expand All @@ -161,11 +212,13 @@ private String getPath(UUID networkUuid, boolean isRegexPhat) {
return "/v1/filters/export?networkUuid=" + networkUuid + "&variantId=variant_1&ids=";
}

abstract void createEquipments();
protected abstract void createEquipments();

protected abstract List<FilterEquipments> getTestFilters();

abstract List<FilterEquipments> getTestFilters();
protected abstract List<FormulaInfos> getFormulaInfos();

abstract List<FormulaInfos> getFormulaInfos();
protected abstract List<FormulaInfos> getUpdatedFormulaInfos();

abstract List<FormulaInfos> getUpdatedFormulaInfos();
protected abstract IdentifiableType getIdentifiableType();
}
Loading

0 comments on commit 088cc84

Please sign in to comment.