Skip to content

Commit

Permalink
refactorings to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
CBiasuzzi committed Jan 25, 2018
1 parent 3939701 commit e861844
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package eu.itesla_project.iidm.eurostag.export;

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.util.ConnectedComponents;

import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -37,14 +38,18 @@ public static String getLoadId(DanglingLine dl) {
return dl.getId() + "_LOAD";
}

public static Bus getBus(Terminal t, EurostagEchExportConfig config) {
if (config.isNoSwitch()) {
public static Bus getBus(Terminal t, boolean noswitch) {
if (noswitch) {
return t.getBusView().getBus();
} else {
return t.getBusBreakerView().getBus();
}
}

public static Bus getBus(Terminal t, EurostagEchExportConfig config) {
return EchUtil.getBus(t, config.isNoSwitch());
}

public static Iterable<Bus> getBuses(Network n, EurostagEchExportConfig config) {
if (config.isNoSwitch()) {
return n.getBusView().getBuses();
Expand Down Expand Up @@ -166,4 +171,16 @@ private static Bus selectSlackbusCriteria1(Network network, EurostagEchExportCon
.orElse(null);
}

public static boolean isInMainCc(Bus bus) {
return ConnectedComponents.getCcNum(bus) == Component.MAIN_NUM;
}

public static boolean isInMainCc(Injection injection, boolean noswitch) {
return ConnectedComponents.getCcNum(EchUtil.getBus(injection.getTerminal(), noswitch)) == Component.MAIN_NUM;
}

public static boolean isInMainCc(Branch branch, boolean noswitch) {
return (ConnectedComponents.getCcNum(EchUtil.getBus(branch.getTerminal1(), noswitch)) == Component.MAIN_NUM)
&& (ConnectedComponents.getCcNum(EchUtil.getBus(branch.getTerminal2(), noswitch)) == Component.MAIN_NUM);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.powsybl.iidm.network.util.ConnectedComponents;
import eu.itesla_project.eurostag.network.Esg8charName;
import eu.itesla_project.eurostag.network.EsgBranchName;
import com.powsybl.iidm.network.*;
Expand Down Expand Up @@ -64,7 +63,7 @@ public static EurostagDictionary create(Network network, BranchParallelIndexes p

for (DanglingLine dl : Identifiables.sort(network.getDanglingLines())) {
// skip if not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(dl.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(dl, config.isNoSwitch())) {
continue;
}
ConnectionBus bus1 = ConnectionBus.fromTerminal(dl.getTerminal(), config, fakeNodes);
Expand All @@ -79,8 +78,7 @@ public static EurostagDictionary create(Network network, BranchParallelIndexes p
Bus bus1 = EchUtil.getBus1(vl, sw.getId(), config);
Bus bus2 = EchUtil.getBus2(vl, sw.getId(), config);
// skip switches not in the main connected component
if (config.isExportMainCCOnly() && ((ConnectedComponents.getCcNum(bus1) != Component.MAIN_NUM)
|| (ConnectedComponents.getCcNum(bus2) != Component.MAIN_NUM))) {
if (config.isExportMainCCOnly() && (!EchUtil.isInMainCc(bus1) || !EchUtil.isInMainCc(bus2))) {
continue;
}
dictionary.addIfNotExist(sw.getId(),
Expand All @@ -92,8 +90,7 @@ public static EurostagDictionary create(Network network, BranchParallelIndexes p

for (Line l : Identifiables.sort(network.getLines())) {
// skip lines not in the main connected component
if (config.isExportMainCCOnly() && ((ConnectedComponents.getCcNum(EchUtil.getBus(l.getTerminal1(), config)) != Component.MAIN_NUM)
|| (ConnectedComponents.getCcNum(EchUtil.getBus(l.getTerminal2(), config)) != Component.MAIN_NUM))) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(l, config.isNoSwitch())) {
continue;
}
ConnectionBus bus1 = ConnectionBus.fromTerminal(l.getTerminal1(), config, fakeNodes);
Expand All @@ -106,8 +103,7 @@ public static EurostagDictionary create(Network network, BranchParallelIndexes p

for (TwoWindingsTransformer twt : Identifiables.sort(network.getTwoWindingsTransformers())) {
// skip transformers not in the main connected component
if (config.isExportMainCCOnly() && ((ConnectedComponents.getCcNum(EchUtil.getBus(twt.getTerminal1(), config)) != Component.MAIN_NUM)
|| (ConnectedComponents.getCcNum(EchUtil.getBus(twt.getTerminal2(), config)) != Component.MAIN_NUM))) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(twt, config.isNoSwitch())) {
continue;
}
ConnectionBus bus1 = ConnectionBus.fromTerminal(twt.getTerminal1(), config, fakeNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.google.common.base.Strings;
import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.util.ConnectedComponents;
import com.powsybl.iidm.network.util.Identifiables;
import eu.itesla_project.eurostag.network.*;
import eu.itesla_project.eurostag.network.io.EsgWriter;
Expand Down Expand Up @@ -104,15 +103,15 @@ private void createNodes(EsgNetwork esgNetwork) {
LOGGER.debug("Slack bus: {} ({})", sb, sb.getVoltageLevel().getId());
for (Bus b : Identifiables.sort(EchUtil.getBuses(network, config))) {
// skip buses not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(b) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(b)) {
LOGGER.warn("not in main component, skipping Bus: {}", b.getId());
continue;
}
esgNetwork.addNode(createNode(b.getId(), b.getVoltageLevel(), b.getV(), b.getAngle(), sb == b));
}
for (DanglingLine dl : Identifiables.sort(network.getDanglingLines())) {
// skip DLs not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(dl.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(dl, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping DanglingLine: {}", dl.getId());
continue;
}
Expand Down Expand Up @@ -142,8 +141,7 @@ private void createCouplingDevices(EsgNetwork esgNetwork) {
Bus bus1 = EchUtil.getBus1(vl, sw.getId(), config);
Bus bus2 = EchUtil.getBus2(vl, sw.getId(), config);
// skip switches not in the main connected component
if (config.isExportMainCCOnly() && ((ConnectedComponents.getCcNum(bus1) != Component.MAIN_NUM)
|| (ConnectedComponents.getCcNum(bus2) != Component.MAIN_NUM))) {
if (config.isExportMainCCOnly() && (!EchUtil.isInMainCc(bus1) || !EchUtil.isInMainCc(bus2))) {
LOGGER.warn("not in main component, skipping Switch: {} {} {}", bus1.getId(), bus2.getId(), sw.getId());
continue;
}
Expand Down Expand Up @@ -192,8 +190,7 @@ private EsgDissymmetricalBranch createDissymmetricalBranch(String id, Connection
private void createLines(EsgNetwork esgNetwork, EsgGeneralParameters parameters) {
for (Line l : Identifiables.sort(network.getLines())) {
// skip lines not in the main connected component
if (config.isExportMainCCOnly() && ((ConnectedComponents.getCcNum(EchUtil.getBus(l.getTerminal1(), config)) != Component.MAIN_NUM)
|| (ConnectedComponents.getCcNum(EchUtil.getBus(l.getTerminal2(), config)) != Component.MAIN_NUM))) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(l, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping Line: {}", l.getId());
continue;
}
Expand Down Expand Up @@ -222,7 +219,7 @@ private void createLines(EsgNetwork esgNetwork, EsgGeneralParameters parameters)
}
for (DanglingLine dl : Identifiables.sort(network.getDanglingLines())) {
// skip if not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(dl.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(dl, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping DanglingLine: {}", dl.getId());
continue;
}
Expand Down Expand Up @@ -294,8 +291,7 @@ private void createTransformers(EsgNetwork esgNetwork, EsgGeneralParameters para

for (TwoWindingsTransformer twt : Identifiables.sort(network.getTwoWindingsTransformers())) {
// skip transformers not in the main connected component
if (config.isExportMainCCOnly() && ((ConnectedComponents.getCcNum(EchUtil.getBus(twt.getTerminal1(), config)) != Component.MAIN_NUM)
|| (ConnectedComponents.getCcNum(EchUtil.getBus(twt.getTerminal2(), config)) != Component.MAIN_NUM))) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(twt, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping TwoWindingsTransformer: {}", twt.getId());
continue;
}
Expand Down Expand Up @@ -463,7 +459,7 @@ private EsgLoad createLoad(ConnectionBus bus, String loadId, float p0, float q0)
private void createLoads(EsgNetwork esgNetwork) {
for (Load l : Identifiables.sort(network.getLoads())) {
// skip loads not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(l.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(l, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping Load: {}", l.getId());
continue;
}
Expand All @@ -472,7 +468,7 @@ private void createLoads(EsgNetwork esgNetwork) {
}
for (DanglingLine dl : Identifiables.sort(network.getDanglingLines())) {
// skip dls not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(dl.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(dl, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping DanglingLine: {}", dl.getId());
continue;
}
Expand All @@ -484,7 +480,7 @@ private void createLoads(EsgNetwork esgNetwork) {
private void createGenerators(EsgNetwork esgNetwork) {
for (Generator g : Identifiables.sort(network.getGenerators())) {
// skip generators not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(g.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(g, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping Generator: {}", g.getId());
continue;
}
Expand Down Expand Up @@ -528,7 +524,7 @@ private void createGenerators(EsgNetwork esgNetwork) {
private void createBanks(EsgNetwork esgNetwork) {
for (ShuntCompensator sc : Identifiables.sort(network.getShunts())) {
// skip shunts not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(sc.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(sc, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping ShuntCompensator: {}", sc.getId());
continue;
}
Expand All @@ -550,7 +546,7 @@ private void createBanks(EsgNetwork esgNetwork) {
private void createStaticVarCompensators(EsgNetwork esgNetwork) {
for (StaticVarCompensator svc : Identifiables.sort(network.getStaticVarCompensators())) {
// skip SVCs not in the main connected component
if (config.isExportMainCCOnly() && (ConnectedComponents.getCcNum(EchUtil.getBus(svc.getTerminal(), config)) != Component.MAIN_NUM)) {
if (config.isExportMainCCOnly() && !EchUtil.isInMainCc(svc, config.isNoSwitch())) {
LOGGER.warn("not in main component, skipping StaticVarCompensator: {}", svc.getId());
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public static EurostagFakeNodes build(Network network, EurostagEchExportConfig c
countUsesMap.getAndIncrement(EchUtil.FAKE_NODE_NAME1);
fakeNodesMap.put(EchUtil.FAKE_NODE_NAME2, EchUtil.FAKE_NODE_NAME2);
countUsesMap.getAndIncrement(EchUtil.FAKE_NODE_NAME2);
Identifiables.sort(network.getVoltageLevels()).stream().forEach(vl -> {
});

Identifiables.sort(network.getVoltageLevels()).stream().map(VoltageLevel::getId).forEach(vlId ->
fakeNodesMap.put(vlId, newEsgId(fakeNodesMap, vlId)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,14 +845,19 @@ private boolean filteredGenerator(Generator g) {
return false;
}

public static Bus getBus(Terminal t, DdExportConfig config) {
if (config.isNoSwitch()) {
public Bus getBus(Terminal t, boolean noswitch) {
if (noswitch) {
return t.getBusView().getBus();
} else {
return t.getBusBreakerView().getBus();
}
}

public boolean isInMainCc(Injection injection, boolean noswitch) {
return ConnectedComponents.getCcNum(getBus(injection.getTerminal(), noswitch)) == Component.MAIN_NUM;
}



/**
* in the workingDir, write eurostag dynamic data (fileName) for a given list of cimIDs, and regulator files (.fri, .frm, .par, .pcp, rcp)
Expand Down Expand Up @@ -906,7 +911,7 @@ public void dumpDtaFile(Path workingDir, String fileName,
log.warn("Skipped generators: network, generator Id, min P, P, max P");
for (Generator g : Identifiables.sort(network.getGenerators())) {
// skip generators not in the main connected component
if (configExport.isExportMainCCOnly() && (ConnectedComponents.getCcNum(getBus(g.getTerminal(), configExport)) != Component.MAIN_NUM)) {
if (configExport.isExportMainCCOnly() && !isInMainCc(g, configExport.isNoSwitch())) {
log.warn("not in main component, skipping Generator: {}", g.getId());
continue;
}
Expand All @@ -921,7 +926,7 @@ public void dumpDtaFile(Path workingDir, String fileName,
//collects SVCs ids, filtering out the regulationMode OFF cases
for (StaticVarCompensator svc : Identifiables.sort(network.getStaticVarCompensators())) {
// skip SVCs not in the main connected component
if (configExport.isExportMainCCOnly() && (ConnectedComponents.getCcNum(getBus(svc.getTerminal(), configExport)) != Component.MAIN_NUM)) {
if (configExport.isExportMainCCOnly() && !isInMainCc(svc, configExport.isNoSwitch())) {
log.warn("not in main component, skipping StaticVarCompensator: {}", svc.getId());
continue;
}
Expand Down

0 comments on commit e861844

Please sign in to comment.