Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Jeandemange <damien.jeandemange@artelys.com>
  • Loading branch information
jeandemanged committed Nov 15, 2024
1 parent 6c4e5aa commit 3bae072
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class MetrixNetwork {
private static final Logger LOGGER = LoggerFactory.getLogger(MetrixNetwork.class);
private static final String PAYS_CVG_PROPERTY = "paysCvg";
private static final String PAYS_CVG_UNDEFINED = "Undefined";
private static final String METRIX_T3WT_STAR_BUS_SUFFIX = "_metrixT3wtStarBus";
private static final String METRIX_T3WT_LEG_SUFFIX = "_metrixT3wtLeg";
private static final String METRIX_DANGLING_LINE_BUS_SUFFIX = "_metrixDanglingLineBus";
private static final String METRIX_DANGLING_LINE_LOAD_SUFFIX = "_metrixDanglingLineLoad";

Expand All @@ -54,6 +56,7 @@ public class MetrixNetwork {
private final Set<Line> lineList = new LinkedHashSet<>();
private final Set<TwoWindingsTransformer> twoWindingsTransformerList = new LinkedHashSet<>();
private final Set<ThreeWindingsTransformer> threeWindingsTransformerList = new LinkedHashSet<>();
private final Set<ThreeWindingsTransformer.Leg> threeWindingsTransformerLegsList = new LinkedHashSet<>();
private final Set<Switch> switchList = new LinkedHashSet<>();
private final Set<DanglingLine> unpairedDanglingLineList = new LinkedHashSet<>();
private final Set<TieLine> tieLineList = new LinkedHashSet<>();
Expand All @@ -71,7 +74,6 @@ public class MetrixNetwork {
private final Map<String, String> mappedSwitchMap = new HashMap<>();

protected MetrixNetwork(Network network) {
// TODO: switch T3T for 3xTWT in the network using a network modification
this.network = Objects.requireNonNull(network);
}

Expand Down Expand Up @@ -107,6 +109,10 @@ public List<ThreeWindingsTransformer> getThreeWindingsTransformerList() {
return List.copyOf(threeWindingsTransformerList);
}

public List<ThreeWindingsTransformer.Leg> getThreeWindingsTransformerLegsList() {
return List.copyOf(threeWindingsTransformerLegsList);
}

public List<DanglingLine> getUnpairedDanglingLineList() {
return List.copyOf(unpairedDanglingLineList);
}
Expand Down Expand Up @@ -144,6 +150,20 @@ public int getIndex(Identifiable<?> identifiable) {
return mapper.getInt(subset, identifiable.getId());
}

public int getThreeWindingsTransformerLegIndex(ThreeWindingsTransformer.Leg leg) {
Objects.requireNonNull(leg);
return mapper.getInt(MetrixSubset.QUAD, getThreeWindingsTransformerLegId(leg));
}

public static String getThreeWindingsTransformerLegId(ThreeWindingsTransformer.Leg leg) {
return leg.getTransformer().getId() + METRIX_T3WT_LEG_SUFFIX + leg.getSide().getNum();
}

public int getThreeWindingsTransformerStarBusIndex(ThreeWindingsTransformer t3wt) {
Objects.requireNonNull(t3wt);
return mapper.getInt(MetrixSubset.NOEUD, t3wt.getId() + METRIX_T3WT_STAR_BUS_SUFFIX);
}

public int getUnpairedDanglingLineBusIndex(DanglingLine danglingLine) {
Objects.requireNonNull(danglingLine);
if (danglingLine.isPaired()) {
Expand Down Expand Up @@ -239,7 +259,13 @@ private void addTwoWindingsTransformer(TwoWindingsTransformer twt) {

private void addThreeWindingsTransformer(ThreeWindingsTransformer twt) {
if (threeWindingsTransformerList.add(twt)) {
mapper.newInt(MetrixSubset.QUAD, twt.getId());
mapper.newInt(MetrixSubset.NOEUD, twt.getId() + METRIX_T3WT_STAR_BUS_SUFFIX);
}
}

private void addThreeWindingsTransformerLeg(ThreeWindingsTransformer.Leg leg) {
if (threeWindingsTransformerLegsList.add(leg)) {
mapper.newInt(MetrixSubset.QUAD, getThreeWindingsTransformerLegId(leg));
}
}

Expand Down Expand Up @@ -275,6 +301,12 @@ private void addPhaseTapChanger(TwoWindingsTransformer twt) {
}
}

private void addPhaseTapChanger(ThreeWindingsTransformer.Leg leg) {
if (phaseTapChangerList.add(leg.getPhaseTapChanger())) {
mapper.newInt(MetrixSubset.DEPHA, getThreeWindingsTransformerLegId(leg));
}
}

private void addBus(Bus bus) {
if (busList.add(bus)) {
mapper.newInt(MetrixSubset.NOEUD, bus.getId());
Expand Down Expand Up @@ -409,12 +441,25 @@ private void createThreeWindingsTransformersList() {
Bus b1 = t1.getBusBreakerView().getBus();
Bus b2 = t2.getBusBreakerView().getBus();
Bus b3 = t3.getBusBreakerView().getBus();
if (b1 != null && b2 != null && b3 != null) {
if (busList.contains(b1) && busList.contains(b2) && busList.contains(b3)) {
addThreeWindingsTransformer(twt);
} else {
nbNok++;
}
List<ThreeWindingsTransformer.Leg> connectedLegs = new ArrayList<>();
if (b1 != null && busList.contains(b1)) {
connectedLegs.add(leg1);
}
if (b2 != null && busList.contains(b2)) {
connectedLegs.add(leg2);
}
if (b3 != null && busList.contains(b3)) {
connectedLegs.add(leg3);
}
if (connectedLegs.size() >= 2) {
// need at least two legs for a flow to happen
addThreeWindingsTransformer(twt);
connectedLegs.forEach(leg -> {
addThreeWindingsTransformerLeg(leg);
if (leg.hasPhaseTapChanger()) {
addPhaseTapChanger(leg);
}
});
} else {
nbNok++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void createMetrixInputs() {
// Quadripoles are lines, transformers and switches
cqnbquad = metrixNetwork.getLineList().size()
+ metrixNetwork.getTwoWindingsTransformerList().size()
+ 3 * metrixNetwork.getThreeWindingsTransformerList().size()
+ metrixNetwork.getThreeWindingsTransformerLegsList().size()
+ metrixNetwork.getSwitchList().size()
+ metrixNetwork.getUnpairedDanglingLineList().size()
+ metrixNetwork.getTieLineList().size();
Expand All @@ -198,7 +198,9 @@ private void createMetrixInputs() {

dcnblies = metrixNetwork.getHvdcLineList().size();

tnnbntot = metrixNetwork.getBusList().size() + metrixNetwork.getUnpairedDanglingLineList().size();
tnnbntot = metrixNetwork.getBusList().size()
+ metrixNetwork.getUnpairedDanglingLineList().size() // boundary bus
+ metrixNetwork.getThreeWindingsTransformerList().size(); // star bus

if (dslData != null) {
sectnbse = dslData.getSectionList().size();
Expand Down Expand Up @@ -396,10 +398,9 @@ private void writeBranches(boolean constantLossFactor, MetrixDie die) {
metrixNetwork.getTwoWindingsTransformerList().forEach(twoWindingsTransformer ->
writeTwoWindingsTransformer(twoWindingsTransformer, metrixInputBranch, metrixInputPhaseTapChanger, constantLossFactor, dtlowran, dtuppran, dttapdep));

// Three Windings Transformers
metrixNetwork.getThreeWindingsTransformerList().forEach(twt -> {
throw new PowsyblException("Three Windings Transformers are not yet supported in metrix");
});
// Three Windings Transformers Legs
metrixNetwork.getThreeWindingsTransformerLegsList().forEach(t3wtLeg ->
writeThreeWindingsTransformerLeg(t3wtLeg, metrixInputBranch, metrixInputPhaseTapChanger, constantLossFactor, dtlowran, dtuppran, dttapdep));

// Switches
metrixNetwork.getSwitchList().forEach(sw -> writeSwitch(sw, metrixInputBranch));
Expand Down Expand Up @@ -502,7 +503,7 @@ private void writeTwoWindingsTransformer(TwoWindingsTransformer twt,
ptc.getLowTapPosition()));
}

//Per-unitage
// Per-uniting
double admittance = toAdmittance(twt.getId(), x, nominalVoltage2, parameters.getNominalU());
r = (r * Math.pow(parameters.getNominalU(), 2)) / Math.pow(nominalVoltage2, 2);

Expand All @@ -513,6 +514,59 @@ private void writeTwoWindingsTransformer(TwoWindingsTransformer twt,
new BranchValues(twt.getId(), admittance, r, getMonitoringTypeBasecase(twt.getId()), getMonitoringTypeOnContingency(twt.getId()), bus1Index, bus2Index));
}

private void writeThreeWindingsTransformerLeg(ThreeWindingsTransformer.Leg leg,
MetrixInputBranch metrixInputBranch,
MetrixInputPhaseTapChanger metrixInputPhaseTapChanger,
boolean constantLossFactor,
List<Integer> dtlowran,
List<Integer> dtuppran,
List<Float> dttapdep) {
double nominalVoltage2 = leg.getTerminal().getVoltageLevel().getNominalV();
double x = leg.getX();
double r = leg.getR();
String t3wtLegId = MetrixNetwork.getThreeWindingsTransformerLegId(leg);
int index = metrixNetwork.getThreeWindingsTransformerLegIndex(leg);

if (leg.hasPhaseTapChanger()) {
PhaseTapChanger ptc = leg.getPhaseTapChanger();
int position = ptc.getTapPosition();
x = x * (1 + ptc.getStep(position).getX() / 100);
r = r * (1 + ptc.getStep(position).getR() / 100);
if (constantLossFactor) {
float val = (float) (Math.pow(x, 2) + Math.pow(r, 2) - Math.pow(leg.getR(), 2));
if (val >= 0) {
x = (float) Math.sqrt(val);
}
LOGGER.debug("constantLossFactor -> t3wt <{} leg {}> x = <{}>", leg.getTransformer().getId(), leg.getSide(), x);
}

MetrixPtcControlType mode = MetrixPtcControlType.FIXED_ANGLE_CONTROL; // FIXME-TODO = getMetrixPtcControlType(leg, index, dtlowran, dtuppran);

for (int pos = ptc.getLowTapPosition(); pos < ptc.getLowTapPosition() + ptc.getStepCount(); pos++) {
dttapdep.add((float) ptc.getStep(pos).getAlpha());
}

writePhaseTapChanger(metrixInputPhaseTapChanger,
metrixNetwork.getIndex(MetrixSubset.DEPHA, t3wtLegId),
new PhaseTapChangerValues(index, mode.getType(),
(float) ptc.getStep(ptc.getLowTapPosition()).getAlpha(),
(float) ptc.getStep(ptc.getHighTapPosition()).getAlpha(),
(float) ptc.getStep(ptc.getTapPosition()).getAlpha(),
ptc.getStepCount(),
ptc.getLowTapPosition()));
}

// Per-uniting
double admittance = toAdmittance(t3wtLegId, x, nominalVoltage2, parameters.getNominalU());
r = (r * Math.pow(parameters.getNominalU(), 2)) / Math.pow(nominalVoltage2, 2);

int bus1Index = metrixNetwork.getThreeWindingsTransformerStarBusIndex(leg.getTransformer());
int bus2Index = metrixNetwork.getIndex(leg.getTerminal().getBusBreakerView().getBus());
writeBranch(metrixInputBranch,
index,
new BranchValues(t3wtLegId, admittance, r, getMonitoringTypeBasecase(t3wtLegId), getMonitoringTypeOnContingency(t3wtLegId), bus1Index, bus2Index));
}

private MetrixPtcControlType getMetrixPtcControlType(TwoWindingsTransformer twt,
int index,
List<Integer> dtlowran,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.google.common.collect.Range;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.commons.PowsyblException;
import com.powsybl.contingency.*;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.test.DanglingLineNetworkFactory;
Expand Down Expand Up @@ -165,9 +164,7 @@ void metrixInputDataWithT3TTest() throws IOException {
Network n = ThreeWindingsTransformerNetworkFactory.create();
MetrixInputData metrixInputData = new MetrixInputData(MetrixNetwork.create(n), null, new MetrixParameters());
try (StringWriter writer = new StringWriter()) {
assertThrows(PowsyblException.class,
() -> metrixInputData.writeJson(writer),
"Three Windings Transformers are not yet supported in metrix");
assertDoesNotThrow(() -> metrixInputData.writeJson(writer)); // TODO test this better ^^
}
}

Expand Down

0 comments on commit 3bae072

Please sign in to comment.