Skip to content

Commit

Permalink
Pretty code
Browse files Browse the repository at this point in the history
Signed-off-by: marquesja1 <[email protected]>
  • Loading branch information
marqueslanauja committed Dec 12, 2024
1 parent a23bad2 commit eee776c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
*/
public class EnergyConsumerConversion extends AbstractConductingEquipmentConversion {

private static final String P_FIXED = "pFixed";
private static final String Q_FIXED = "qFixed";

public EnergyConsumerConversion(PropertyBag ec, Context context) {
super(CgmesNames.ENERGY_CONSUMER, ec, context);
loadKind = ec.getLocal("type");
Expand All @@ -44,8 +41,8 @@ public void convert() {
} else {
loadType = LoadType.UNDEFINED;
}
double pFixed = p.asDouble(P_FIXED, 0.0);
double qFixed = p.asDouble(Q_FIXED, 0.0);
double pFixed = p.asDouble(CgmesNames.P_FIXED, 0.0);
double qFixed = p.asDouble(CgmesNames.Q_FIXED, 0.0);
LoadAdder adder = voltageLevel().newLoad()
.setLoadType(loadType);
identify(adder);
Expand All @@ -61,8 +58,8 @@ public void convert() {

private void addSpecificProperties(Load newLoad, double pFixed, double qFixed) {
newLoad.setProperty(Conversion.PROPERTY_CGMES_ORIGINAL_CLASS, loadKind);
newLoad.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + P_FIXED, String.valueOf(pFixed));
newLoad.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + Q_FIXED, String.valueOf(qFixed));
newLoad.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + CgmesNames.P_FIXED, String.valueOf(pFixed));
newLoad.setProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + CgmesNames.Q_FIXED, String.valueOf(qFixed));
}

private void model(LoadAdder adder) {
Expand Down Expand Up @@ -145,8 +142,8 @@ private static void setLoadDetail(String type, Load newLoad, double pFixed, doub
public static void update(Load load, PropertyBag cgmesData, Context context) {
updateTerminals(load, context, load.getTerminal());

double pFixed = Double.parseDouble(load.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + P_FIXED));
double qFixed = Double.parseDouble(load.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + Q_FIXED));
double pFixed = Double.parseDouble(load.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + CgmesNames.P_FIXED));
double qFixed = Double.parseDouble(load.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + CgmesNames.Q_FIXED));

PowerFlow updatedPowerFlow = updatedPowerFlow(load, cgmesData, context);
load.setP0(updatedPowerFlow.defined() ? updatedPowerFlow.p() : defaultValue(pFixed, load.getP0(), 0.0, Double.NaN, getDefaultValueSelector(context)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package com.powsybl.cgmes.conversion.test;

import com.powsybl.cgmes.conversion.Conversion;
import com.powsybl.cgmes.model.CgmesNames;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.Network;
import org.junit.jupiter.api.Test;
Expand All @@ -28,13 +30,13 @@ void importEqTest() {
assertEquals(3, network.getLoadCount());

Load loadEnergyConsumer = network.getLoad("EnergyConsumer");
assertTrue(checkNaNLoad(loadEnergyConsumer));
assertTrue(checkEq(loadEnergyConsumer));

Load loadEnergySource = network.getLoad("EnergySource");
assertTrue(checkNaNLoad(loadEnergySource));
assertTrue(checkEq(loadEnergySource));

Load loadAsynchronousMachine = network.getLoad("AsynchronousMachine");
assertTrue(checkNaNLoad(loadAsynchronousMachine));
assertTrue(checkEq(loadAsynchronousMachine));
}

@Test
Expand All @@ -43,67 +45,73 @@ void importEqAndSshTogetherTest() {
assertEquals(3, network.getLoadCount());

Load loadEnergyConsumer = network.getLoad("EnergyConsumer");
assertTrue(checkLoad(loadEnergyConsumer, 10.0, 5.0));
assertTrue(checkSsh(loadEnergyConsumer, 10.0, 5.0));

Load loadEnergySource = network.getLoad("EnergySource");
assertTrue(checkLoad(loadEnergySource, -200.0, -90.0));
assertTrue(checkSsh(loadEnergySource, -200.0, -90.0));

Load loadAsynchronousMachine = network.getLoad("AsynchronousMachine");
assertTrue(checkLoad(loadAsynchronousMachine, 200.0, 50.0));
assertTrue(checkSsh(loadAsynchronousMachine, 200.0, 50.0));
}

@Test
void importEAndSshSeparatelyTest() {
void importEqAndSshSeparatelyTest() {
Network network = readCgmesResources(DIR, "load_EQ.xml");
assertEquals(3, network.getLoadCount());

Load loadEnergyConsumer = network.getLoad("EnergyConsumer");
assertTrue(checkNaNLoad(loadEnergyConsumer));
assertTrue(checkEq(loadEnergyConsumer));
Load loadEnergySource = network.getLoad("EnergySource");
assertTrue(checkNaNLoad(loadEnergySource));
assertTrue(checkEq(loadEnergySource));
Load loadAsynchronousMachine = network.getLoad("AsynchronousMachine");
assertTrue(checkNaNLoad(loadAsynchronousMachine));
assertTrue(checkEq(loadAsynchronousMachine));

readCgmesResources(network, DIR, "load_SSH.xml");

assertTrue(checkLoad(loadEnergyConsumer, 10.0, 5.0));
assertTrue(checkLoad(loadEnergySource, -200.0, -90.0));
assertTrue(checkLoad(loadAsynchronousMachine, 200.0, 50.0));
assertTrue(checkSsh(loadEnergyConsumer, 10.0, 5.0));
assertTrue(checkSsh(loadEnergySource, -200.0, -90.0));
assertTrue(checkSsh(loadAsynchronousMachine, 200.0, 50.0));
}

@Test
void importEAndTwoSshsTest() {
void importEqAndTwoSshsTest() {
Network network = readCgmesResources(DIR, "load_EQ.xml");
assertEquals(3, network.getLoadCount());

Load loadEnergyConsumer = network.getLoad("EnergyConsumer");
assertTrue(checkNaNLoad(loadEnergyConsumer));
assertTrue(checkEq(loadEnergyConsumer));
Load loadEnergySource = network.getLoad("EnergySource");
assertTrue(checkNaNLoad(loadEnergySource));
assertTrue(checkEq(loadEnergySource));
Load loadAsynchronousMachine = network.getLoad("AsynchronousMachine");
assertTrue(checkNaNLoad(loadAsynchronousMachine));
assertTrue(checkEq(loadAsynchronousMachine));

readCgmesResources(network, DIR, "load_SSH.xml");

assertTrue(checkLoad(loadEnergyConsumer, 10.0, 5.0));
assertTrue(checkLoad(loadEnergySource, -200.0, -90.0));
assertTrue(checkLoad(loadAsynchronousMachine, 200.0, 50.0));
assertTrue(checkSsh(loadEnergyConsumer, 10.0, 5.0));
assertTrue(checkSsh(loadEnergySource, -200.0, -90.0));
assertTrue(checkSsh(loadAsynchronousMachine, 200.0, 50.0));

readCgmesResources(network, DIR, "load_SSH_1.xml");

assertTrue(checkLoad(loadEnergyConsumer, 10.5, 5.5));
assertTrue(checkLoad(loadEnergySource, -200.5, -90.5));
assertTrue(checkLoad(loadAsynchronousMachine, 200.5, 50.5));
assertTrue(checkSsh(loadEnergyConsumer, 10.5, 5.5));
assertTrue(checkSsh(loadEnergySource, -200.5, -90.5));
assertTrue(checkSsh(loadAsynchronousMachine, 200.5, 50.5));
}

private static boolean checkNaNLoad(Load load) {
private static boolean checkEq(Load load) {
assertNotNull(load);
assertTrue(Double.isNaN(load.getP0()));
assertTrue(Double.isNaN(load.getQ0()));
String originalClass = load.getProperty(Conversion.PROPERTY_CGMES_ORIGINAL_CLASS);
if (originalClass.equals(CgmesNames.ENERGY_CONSUMER)) {
assertNotNull(load.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + CgmesNames.P_FIXED));
assertNotNull(load.getProperty(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + CgmesNames.Q_FIXED));
}
load.getPropertyNames().forEach(pn -> System.err.printf("Property %s %s %n", pn, load.getProperty(pn)));
return true;
}

private static boolean checkLoad(Load load, double p0, double q0) {
private static boolean checkSsh(Load load, double p0, double q0) {
assertNotNull(load);
assertEquals(p0, load.getP0());
assertEquals(q0, load.getQ0());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public final class CgmesNames {
public static final String CURRENT_LIMIT = "CurrentLimit";
public static final String ACTIVE_POWER_LIMIT = "ActivePowerLimit";
public static final String APPARENT_POWER_LIMIT = "ApparentPowerLimit";
public static final String P_FIXED = "pFixed";
public static final String Q_FIXED = "qFixed";

private CgmesNames() {
}
Expand Down

0 comments on commit eee776c

Please sign in to comment.