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

Add equipments to modif by formula #379

Merged
merged 15 commits into from
Nov 24, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.ShuntCompensator;
import com.powsybl.iidm.network.VoltageLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -20,7 +22,10 @@
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.LoadField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.VoltageLevelField;


/**
* @author Seddik Yengui <Seddik.yengui at rte-france.com>
Expand Down Expand Up @@ -51,6 +56,8 @@ public Double getRefOrValue(Identifiable<?> identifiable) {
case GENERATOR -> GeneratorField.getReferenceValue((Generator) identifiable, equipmentField);
case BATTERY -> BatteryField.getReferenceValue((Battery) identifiable, equipmentField);
case SHUNT_COMPENSATOR -> ShuntCompensatorField.getReferenceValue((ShuntCompensator) identifiable, equipmentField);
case VOLTAGE_LEVEL -> VoltageLevelField.getReferenceValue((VoltageLevel) identifiable, equipmentField);
case LOAD -> LoadField.getReferenceValue((Load) 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,35 @@
/**
* 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.Load;

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

public enum LoadField {
ACTIVE_POWER,
REACTIVE_POWER;

public static Double getReferenceValue(Load load, String loadField) {
LoadField field = LoadField.valueOf(loadField);
return switch (field) {
case ACTIVE_POWER -> load.getP0();
case REACTIVE_POWER -> load.getQ0();
};
}

public static void setNewValue(Load load, String loadField, Double newValue) {
LoadField field = LoadField.valueOf(loadField);
switch (field) {
case ACTIVE_POWER -> load.setP0(newValue);
case REACTIVE_POWER -> load.setQ0(newValue);
}
}
}
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.VoltageLevel;
import com.powsybl.iidm.network.extensions.IdentifiableShortCircuit;
import com.powsybl.iidm.network.extensions.IdentifiableShortCircuitAdder;

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

public enum VoltageLevelField {
NOMINAL_VOLTAGE,
LOW_VOLTAGE_LIMIT,
HIGH_VOLTAGE_LIMIT,
LOW_SHORT_CIRCUIT_CURRENT_LIMIT,
HIGH_SHORT_CIRCUIT_CURRENT_LIMIT;

public static Double getReferenceValue(VoltageLevel voltageLevel, String voltageLevelField) {
IdentifiableShortCircuit<VoltageLevel> identifiableShortCircuit = voltageLevel.getExtension(IdentifiableShortCircuit.class);
VoltageLevelField field = VoltageLevelField.valueOf(voltageLevelField);
return switch (field) {
case NOMINAL_VOLTAGE -> voltageLevel.getNominalV();
case LOW_VOLTAGE_LIMIT -> voltageLevel.getLowVoltageLimit();
case HIGH_VOLTAGE_LIMIT -> voltageLevel.getHighVoltageLimit();
case LOW_SHORT_CIRCUIT_CURRENT_LIMIT -> identifiableShortCircuit == null ? null : identifiableShortCircuit.getIpMin();
case HIGH_SHORT_CIRCUIT_CURRENT_LIMIT -> identifiableShortCircuit == null ? null : identifiableShortCircuit.getIpMax();
};
}

public static void setNewValue(VoltageLevel voltageLevel, String voltageLevelField, Double newValue) {
IdentifiableShortCircuit<VoltageLevel> identifiableShortCircuit = voltageLevel.getExtension(IdentifiableShortCircuit.class);
VoltageLevelField field = VoltageLevelField.valueOf(voltageLevelField);
switch (field) {
case NOMINAL_VOLTAGE -> voltageLevel.setNominalV(newValue);
case LOW_VOLTAGE_LIMIT -> voltageLevel.setLowVoltageLimit(newValue);
case HIGH_VOLTAGE_LIMIT -> voltageLevel.setHighVoltageLimit(newValue);
case LOW_SHORT_CIRCUIT_CURRENT_LIMIT -> {
IdentifiableShortCircuitAdder<VoltageLevel> adder = voltageLevel.newExtension(IdentifiableShortCircuitAdder.class).withIpMin(newValue);
if (identifiableShortCircuit != null) {
adder.withIpMax(identifiableShortCircuit.getIpMax());
}
adder.add();
}
case HIGH_SHORT_CIRCUIT_CURRENT_LIMIT -> {
IdentifiableShortCircuitAdder<VoltageLevel> adder = voltageLevel.newExtension(IdentifiableShortCircuitAdder.class).withIpMax(newValue);
if (identifiableShortCircuit != null) {
adder.withIpMin(identifiableShortCircuit.getIpMin());
}
adder.add();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.powsybl.iidm.network.Battery;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.ShuntCompensator;
import com.powsybl.iidm.network.VoltageLevel;
import org.gridsuite.modification.server.NetworkModificationException;
import org.gridsuite.modification.server.dto.ByFormulaModificationInfos;
import org.gridsuite.modification.server.dto.FilterEquipments;
Expand All @@ -23,7 +25,9 @@
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.LoadField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField;
import org.gridsuite.modification.server.dto.formula.equipmentfield.VoltageLevelField;
import org.gridsuite.modification.server.service.FilterService;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -155,6 +159,8 @@ private void applyFormula(Network network,
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);
case VOLTAGE_LEVEL -> VoltageLevelField.setNewValue((VoltageLevel) identifiable, formulaInfos.getEditedField(), newValue);
case LOAD -> LoadField.setNewValue((Load) 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 @@ -110,7 +110,7 @@ protected void checkCreateWithError(List<FormulaInfos> formulaInfos) throws Exce
@Override
public void testCreate() throws Exception {
List<FilterEquipments> filters = getTestFilters();
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + ".{2,}"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
.withHeader("Content-Type", "application/json"))).getId();
Expand All @@ -125,7 +125,7 @@ public void testCreate() throws Exception {
public void testCopy() throws Exception {

List<FilterEquipments> filters = getTestFilters();
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + "(.+,){4}.*"))
UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching(getPath(getNetworkUuid(), true) + ".{2,}"))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(filters))
.withHeader("Content-Type", "application/json"))).getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* 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.iidm.network.IdentifiableType;
import org.gridsuite.modification.server.dto.FilterEquipments;
import org.gridsuite.modification.server.dto.IdentifiableAttributes;
import org.gridsuite.modification.server.dto.formula.FormulaInfos;
import org.gridsuite.modification.server.dto.formula.Operator;
import org.gridsuite.modification.server.dto.formula.ReferenceFieldOrValue;
import org.gridsuite.modification.server.dto.formula.equipmentfield.LoadField;

import java.util.List;

import static org.gridsuite.modification.server.utils.NetworkUtil.createLoad;
import static org.junit.Assert.assertEquals;

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

public class LoadByFormulaModificationTest extends AbstractByFormulaModificationTest {
private static final String LOAD_ID_1 = "load1";
private static final String LOAD_ID_2 = "load2";
private static final String LOAD_ID_3 = "load3";
private static final String LOAD_ID_4 = "load4";

@Override
protected void createEquipments() {
createLoad(getNetwork().getVoltageLevel("v1"), LOAD_ID_1, "load1", 100, 100, 120, null, 5, null);
createLoad(getNetwork().getVoltageLevel("v2"), LOAD_ID_2, "load2", 200, 80, 90, null, 5, null);
createLoad(getNetwork().getVoltageLevel("v3"), LOAD_ID_3, "load3", 300, 100, 70, null, 5, null);
createLoad(getNetwork().getVoltageLevel("v4"), LOAD_ID_4, "load4", 400, 50, 150, null, 5, null);
}

@Override
protected List<FilterEquipments> getTestFilters() {
IdentifiableAttributes load1 = getIdentifiableAttributes(LOAD_ID_1, 1.0);
IdentifiableAttributes load2 = getIdentifiableAttributes(LOAD_ID_2, 2.0);
IdentifiableAttributes load3 = getIdentifiableAttributes(LOAD_ID_3, 2.0);
IdentifiableAttributes load4 = getIdentifiableAttributes(LOAD_ID_4, 5.0);

FilterEquipments filter1 = getFilterEquipments(FILTER_ID_1, "filter1", List.of(load1, load2), List.of());
FilterEquipments filter2 = getFilterEquipments(FILTER_ID_2, "filter2", List.of(load3, load4), List.of());

return List.of(filter1, filter2);
}

@Override
protected List<FormulaInfos> getFormulaInfos() {
FormulaInfos formulaInfos1 = getFormulaInfo(LoadField.ACTIVE_POWER.name(),
List.of(filter1),
Operator.ADDITION,
ReferenceFieldOrValue.builder().equipmentField(LoadField.ACTIVE_POWER.name()).build(),
ReferenceFieldOrValue.builder().value(25.).build()
);

FormulaInfos formulaInfos2 = getFormulaInfo(LoadField.REACTIVE_POWER.name(),
List.of(filter2),
Operator.MULTIPLICATION,
ReferenceFieldOrValue.builder().equipmentField(LoadField.REACTIVE_POWER.name()).build(),
ReferenceFieldOrValue.builder().value(2.5).build()
);
return List.of(formulaInfos1, formulaInfos2);
}

@Override
protected List<FormulaInfos> getUpdatedFormulaInfos() {
FormulaInfos formulaInfos1 = getFormulaInfo(LoadField.ACTIVE_POWER.name(),
List.of(filter1),
Operator.PERCENTAGE,
ReferenceFieldOrValue.builder().value(200.).build(),
ReferenceFieldOrValue.builder().equipmentField(LoadField.ACTIVE_POWER.name()).build()
);

return List.of(formulaInfos1);
}

@Override
protected IdentifiableType getIdentifiableType() {
return IdentifiableType.LOAD;
}

@Override
protected void assertAfterNetworkModificationCreation() {
assertEquals(125, getNetwork().getLoad(LOAD_ID_1).getP0(), 0);
assertEquals(105, getNetwork().getLoad(LOAD_ID_2).getP0(), 0);
assertEquals(175, getNetwork().getLoad(LOAD_ID_3).getQ0(), 0);
assertEquals(375, getNetwork().getLoad(LOAD_ID_4).getQ0(), 0);
}

@Override
protected void assertAfterNetworkModificationDeletion() {
assertEquals(100, getNetwork().getLoad(LOAD_ID_1).getP0(), 0);
assertEquals(80, getNetwork().getLoad(LOAD_ID_2).getP0(), 0);
assertEquals(70, getNetwork().getLoad(LOAD_ID_3).getQ0(), 0);
assertEquals(150, getNetwork().getLoad(LOAD_ID_4).getQ0(), 0);
}
}
Loading