Skip to content

Commit

Permalink
Use LfNetworkParameters everywhere to simplify code (#682)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@gmail.com>
  • Loading branch information
geofjamg authored Dec 25, 2022
1 parent 9ed5669 commit 7329c91
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.network.PiModel;
import com.powsybl.openloadflow.util.Evaluable;

Expand All @@ -32,8 +33,8 @@ public abstract class AbstractImpedantLfBranch extends AbstractLfBranch {

protected Evaluable i2 = NAN;

protected AbstractImpedantLfBranch(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, boolean dc, double lowImpedanceThreshold) {
super(network, bus1, bus2, piModel, dc, lowImpedanceThreshold);
protected AbstractImpedantLfBranch(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, LfNetworkParameters parameters) {
super(network, bus1, bus2, piModel, parameters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public abstract class AbstractLfBranch extends AbstractElement implements LfBran

protected boolean zeroImpedance;

protected AbstractLfBranch(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, boolean dc, double lowImpedanceThreshold) {
protected AbstractLfBranch(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, LfNetworkParameters parameters) {
super(network);
this.bus1 = bus1;
this.bus2 = bus2;
this.piModel = Objects.requireNonNull(piModel);
this.piModel.setBranch(this);
zeroImpedance = isZeroImpedanceBranch(piModel, dc, lowImpedanceThreshold);
zeroImpedance = isZeroImpedanceBranch(piModel, parameters);
}

protected static List<LfLimit> createSortedLimitsList(LoadingLimits loadingLimits, LfBus bus) {
Expand Down Expand Up @@ -293,11 +293,11 @@ public void setMinZ(boolean dc, double lowImpedanceThreshold) {
this.zeroImpedance = false;
}

public static boolean isZeroImpedanceBranch(PiModel piModel, boolean dc, double lowImpedanceThreshold) {
if (dc) {
return FastMath.abs(piModel.getX()) < lowImpedanceThreshold;
private static boolean isZeroImpedanceBranch(PiModel piModel, LfNetworkParameters parameters) {
if (parameters.isDc()) {
return FastMath.abs(piModel.getX()) < parameters.getLowImpedanceThreshold();
} else {
return piModel.getZ() < lowImpedanceThreshold;
return piModel.getZ() < parameters.getLowImpedanceThreshold();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.openloadflow.OpenLoadFlowParameters;
import com.powsybl.openloadflow.network.*;
import com.powsybl.openloadflow.util.Evaluable;
import com.powsybl.openloadflow.util.PerUnit;
Expand Down Expand Up @@ -228,18 +227,14 @@ protected void add(LfGenerator generator) {
}
}

void addGenerator(Generator generator, boolean breakers, double plausibleActivePowerLimit, boolean reactiveLimits,
LfNetworkLoadingReport report, double minPlausibleTargetVoltage, double maxPlausibleTargetVoltage, OpenLoadFlowParameters.ReactiveRangeCheckMode reactiveRangeCheckMode) {
add(LfGeneratorImpl.create(generator, network, breakers, plausibleActivePowerLimit, reactiveLimits, report, minPlausibleTargetVoltage, maxPlausibleTargetVoltage, reactiveRangeCheckMode));
void addGenerator(Generator generator, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
add(LfGeneratorImpl.create(generator, network, parameters, report));
}

void addStaticVarCompensator(StaticVarCompensator staticVarCompensator, boolean voltagePerReactivePowerControl,
boolean breakers, boolean reactiveLimits, LfNetworkLoadingReport report,
double minPlausibleTargetVoltage, double maxPlausibleTargetVoltage, OpenLoadFlowParameters.ReactiveRangeCheckMode reactiveRangeCheckMode,
boolean svcMonitoringVoltage) {
void addStaticVarCompensator(StaticVarCompensator staticVarCompensator, LfNetworkParameters parameters,
LfNetworkLoadingReport report) {
if (staticVarCompensator.getRegulationMode() != StaticVarCompensator.RegulationMode.OFF) {
LfStaticVarCompensatorImpl lfSvc = LfStaticVarCompensatorImpl.create(staticVarCompensator, network, this, voltagePerReactivePowerControl,
breakers, reactiveLimits, report, minPlausibleTargetVoltage, maxPlausibleTargetVoltage, reactiveRangeCheckMode, svcMonitoringVoltage);
LfStaticVarCompensatorImpl lfSvc = LfStaticVarCompensatorImpl.create(staticVarCompensator, network, this, parameters, report);
add(lfSvc);
if (lfSvc.getSlope() != 0) {
hasGeneratorsWithSlope = true;
Expand All @@ -250,17 +245,16 @@ void addStaticVarCompensator(StaticVarCompensator staticVarCompensator, boolean
}
}

void addVscConverterStation(VscConverterStation vscCs, boolean breakers, boolean reactiveLimits, LfNetworkLoadingReport report,
double minPlausibleTargetVoltage, double maxPlausibleTargetVoltage, OpenLoadFlowParameters.ReactiveRangeCheckMode reactiveRangeCheckMode) {
add(LfVscConverterStationImpl.create(vscCs, network, breakers, reactiveLimits, report, minPlausibleTargetVoltage, maxPlausibleTargetVoltage, reactiveRangeCheckMode));
void addVscConverterStation(VscConverterStation vscCs, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
add(LfVscConverterStationImpl.create(vscCs, network, parameters, report));
}

void addBattery(Battery generator, double plausibleActivePowerLimit, LfNetworkLoadingReport report) {
add(LfBatteryImpl.create(generator, network, plausibleActivePowerLimit, report));
void addBattery(Battery generator, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
add(LfBatteryImpl.create(generator, network, parameters, report));
}

void setShuntCompensators(List<ShuntCompensator> shuntCompensators, boolean isShuntVoltageControl) {
if (!isShuntVoltageControl && !shuntCompensators.isEmpty()) {
void setShuntCompensators(List<ShuntCompensator> shuntCompensators, LfNetworkParameters parameters) {
if (!parameters.isShuntVoltageControl() && !shuntCompensators.isEmpty()) {
shunt = new LfShuntImpl(shuntCompensators, network, this, false);
} else {
List<ShuntCompensator> controllerShuntCompensators = shuntCompensators.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.openloadflow.OpenLoadFlowParameters;
import com.powsybl.openloadflow.network.*;
import com.powsybl.openloadflow.util.PerUnit;
import org.slf4j.Logger;
Expand Down Expand Up @@ -183,37 +182,36 @@ public LfBus getControlledBus() {
return network.getBusById(controlledBusId);
}

protected void setVoltageControl(double targetV, Terminal terminal, Terminal regulatingTerminal, boolean breakers,
boolean reactiveLimits, LfNetworkLoadingReport report, double minPlausibleTargetVoltage,
double maxPlausibleTargetVoltage, OpenLoadFlowParameters.ReactiveRangeCheckMode reactiveRangeCheckMode) {
if (!checkVoltageControlConsistency(reactiveLimits, report, reactiveRangeCheckMode)) {
protected void setVoltageControl(double targetV, Terminal terminal, Terminal regulatingTerminal, LfNetworkParameters parameters,
LfNetworkLoadingReport report) {
if (!checkVoltageControlConsistency(parameters, report)) {
return;
}
Bus controlledBus = breakers ? regulatingTerminal.getBusBreakerView().getBus() : regulatingTerminal.getBusView().getBus();
Bus controlledBus = parameters.isBreakers() ? regulatingTerminal.getBusBreakerView().getBus() : regulatingTerminal.getBusView().getBus();
if (controlledBus == null) {
LOGGER.warn("Regulating terminal of LfGenerator {} is out of voltage: voltage control discarded", getId());
return;
}
boolean inSameSynchronousComponent = breakers
boolean inSameSynchronousComponent = parameters.isBreakers()
? regulatingTerminal.getBusBreakerView().getBus().getSynchronousComponent().getNum() == terminal.getBusBreakerView().getBus().getSynchronousComponent().getNum()
: regulatingTerminal.getBusView().getBus().getSynchronousComponent().getNum() == terminal.getBusView().getBus().getSynchronousComponent().getNum();
if (!inSameSynchronousComponent) {
LOGGER.warn("Regulating terminal of LfGenerator {} is not in the same synchronous component: voltage control discarded", getId());
return;
}
if (!checkTargetV(targetV / regulatingTerminal.getVoltageLevel().getNominalV(), report, minPlausibleTargetVoltage, maxPlausibleTargetVoltage)) {
if (!checkTargetV(targetV / regulatingTerminal.getVoltageLevel().getNominalV(), parameters, report)) {
return;
}
this.controlledBusId = controlledBus.getId();
this.targetV = targetV / regulatingTerminal.getVoltageLevel().getNominalV();
this.generatorControlType = GeneratorControlType.VOLTAGE;
}

protected boolean checkVoltageControlConsistency(boolean reactiveLimits, LfNetworkLoadingReport report, OpenLoadFlowParameters.ReactiveRangeCheckMode reactiveRangeCheckMode) {
protected boolean checkVoltageControlConsistency(LfNetworkParameters parameters, LfNetworkLoadingReport report) {
boolean consistency = true;
if (reactiveLimits) {
if (parameters.isReactiveLimits()) {
double rangeQ;
switch (reactiveRangeCheckMode) {
switch (parameters.getReactiveRangeCheckMode()) {
case MIN_MAX:
double minRangeQ = getRangeQ(ReactiveRangeMode.MIN);
double maxRangeQ = getRangeQ(ReactiveRangeMode.MAX);
Expand All @@ -240,7 +238,7 @@ protected boolean checkVoltageControlConsistency(boolean reactiveLimits, LfNetwo
}
break;
default:
throw new IllegalStateException("Unknown reactive range check mode: " + reactiveRangeCheckMode);
throw new IllegalStateException("Unknown reactive range check mode: " + parameters.getReactiveRangeCheckMode());
}
}
if (Math.abs(getTargetP()) < POWER_EPSILON_SI && getMinP() > POWER_EPSILON_SI) {
Expand All @@ -251,10 +249,9 @@ protected boolean checkVoltageControlConsistency(boolean reactiveLimits, LfNetwo
return consistency;
}

protected boolean checkTargetV(double targetV, LfNetworkLoadingReport report, double minPlausibleTargetVoltage,
double maxPlausibleTargetVoltage) {
protected boolean checkTargetV(double targetV, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
// check that targetV has a plausible value (wrong nominal voltage issue)
if (targetV < minPlausibleTargetVoltage || targetV > maxPlausibleTargetVoltage) {
if (targetV < parameters.getMinPlausibleTargetVoltage() || targetV > parameters.getMaxPlausibleTargetVoltage()) {
LOGGER.trace("Generator '{}' has an inconsistent target voltage: {} pu: generator voltage control discarded",
getId(), targetV);
report.generatorsWithInconsistentTargetVoltage++;
Expand Down Expand Up @@ -304,7 +301,7 @@ public void setParticipating(boolean participating) {
// nothing to do
}

protected boolean checkActivePowerControl(double targetP, double minP, double maxP, double plausibleActivePowerLimit,
protected boolean checkActivePowerControl(double targetP, double minP, double maxP, LfNetworkParameters parameters,
LfNetworkLoadingReport report) {
boolean participating = true;
if (Math.abs(targetP) < POWER_EPSILON_SI) {
Expand All @@ -325,9 +322,9 @@ protected boolean checkActivePowerControl(double targetP, double minP, double ma
report.generatorsDiscardedFromActivePowerControlBecauseTargetPLowerThanMinP++;
participating = false;
}
if (maxP > plausibleActivePowerLimit) {
if (maxP > parameters.getPlausibleActivePowerLimit()) {
LOGGER.trace("Discard generator '{}' from active power control because maxP ({}) > {}} MW",
getId(), maxP, plausibleActivePowerLimit);
getId(), maxP, parameters.getPlausibleActivePowerLimit());
report.generatorsDiscardedFromActivePowerControlBecauseMaxPNotPlausible++;
participating = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.iidm.network.ReactiveLimits;
import com.powsybl.iidm.network.extensions.ActivePowerControl;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.util.PerUnit;

import java.util.Objects;
Expand All @@ -26,7 +27,7 @@ public final class LfBatteryImpl extends AbstractLfGenerator {

private double droop;

private LfBatteryImpl(Battery battery, LfNetwork network, double plausibleActivePowerLimit, LfNetworkLoadingReport report) {
private LfBatteryImpl(Battery battery, LfNetwork network, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
super(network, battery.getTargetP());
this.batteryRef = new Ref<>(battery);
participating = true;
Expand All @@ -40,15 +41,17 @@ private LfBatteryImpl(Battery battery, LfNetwork network, double plausibleActive
}
}

if (!checkActivePowerControl(battery.getTargetP(), battery.getMinP(), battery.getMaxP(), plausibleActivePowerLimit, report)) {
if (!checkActivePowerControl(battery.getTargetP(), battery.getMinP(), battery.getMaxP(), parameters, report)) {
participating = false;
}
}

public static LfBatteryImpl create(Battery battery, LfNetwork network, double plausibleActivePowerLimit, LfNetworkLoadingReport report) {
public static LfBatteryImpl create(Battery battery, LfNetwork network, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
Objects.requireNonNull(battery);
Objects.requireNonNull(network);
Objects.requireNonNull(parameters);
Objects.requireNonNull(report);
return new LfBatteryImpl(battery, network, plausibleActivePowerLimit, report);
return new LfBatteryImpl(battery, network, parameters, report);
}

private Battery getBattery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class LfBranchImpl extends AbstractImpedantLfBranch {

private final Ref<Branch<?>> branchRef;

protected LfBranchImpl(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, Branch<?> branch, boolean dc, double lowImpedanceThreshold) {
super(network, bus1, bus2, piModel, dc, lowImpedanceThreshold);
protected LfBranchImpl(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, Branch<?> branch, LfNetworkParameters parameters) {
super(network, bus1, bus2, piModel, parameters);
this.branchRef = new Ref<>(branch);
}

private static LfBranchImpl createLine(Line line, LfNetwork network, LfBus bus1, LfBus bus2, double zb, boolean dc, double lowImpedanceThreshold) {
private static LfBranchImpl createLine(Line line, LfNetwork network, LfBus bus1, LfBus bus2, double zb, LfNetworkParameters parameters) {
PiModel piModel = new SimplePiModel()
.setR1(1 / Transformers.getRatioPerUnitBase(line))
.setR(line.getR() / zb)
Expand All @@ -38,11 +38,11 @@ private static LfBranchImpl createLine(Line line, LfNetwork network, LfBus bus1,
.setB1(line.getB1() * zb)
.setB2(line.getB2() * zb);

return new LfBranchImpl(network, bus1, bus2, piModel, line, dc, lowImpedanceThreshold);
return new LfBranchImpl(network, bus1, bus2, piModel, line, parameters);
}

private static LfBranchImpl createTransformer(TwoWindingsTransformer twt, LfNetwork network, LfBus bus1, LfBus bus2, double zb,
boolean twtSplitShuntAdmittance, boolean dc, double lowImpedanceThreshold) {
LfNetworkParameters parameters) {
PiModel piModel = null;

double baseRatio = Transformers.getRatioPerUnitBase(twt);
Expand All @@ -57,7 +57,7 @@ private static LfBranchImpl createTransformer(TwoWindingsTransformer twt, LfNetw
List<PiModel> models = new ArrayList<>();
for (int ptcPosition = ptc.getLowTapPosition(); ptcPosition <= ptc.getHighTapPosition(); ptcPosition++) {
Transformers.TapCharacteristics tapCharacteristics = Transformers.getTapCharacteristics(twt, rtcPosition, ptcPosition);
models.add(Transformers.createPiModel(tapCharacteristics, zb, baseRatio, twtSplitShuntAdmittance));
models.add(Transformers.createPiModel(tapCharacteristics, zb, baseRatio, parameters.isTwtSplitShuntAdmittance()));
}
piModel = new PiModelArray(models, ptc.getLowTapPosition(), ptc.getTapPosition());
}
Expand All @@ -71,7 +71,7 @@ private static LfBranchImpl createTransformer(TwoWindingsTransformer twt, LfNetw
List<PiModel> models = new ArrayList<>();
for (int rtcPosition = rtc.getLowTapPosition(); rtcPosition <= rtc.getHighTapPosition(); rtcPosition++) {
Transformers.TapCharacteristics tapCharacteristics = Transformers.getTapCharacteristics(twt, rtcPosition, ptcPosition);
models.add(Transformers.createPiModel(tapCharacteristics, zb, baseRatio, twtSplitShuntAdmittance));
models.add(Transformers.createPiModel(tapCharacteristics, zb, baseRatio, parameters.isTwtSplitShuntAdmittance()));
}
piModel = new PiModelArray(models, rtc.getLowTapPosition(), rtc.getTapPosition());
} else {
Expand All @@ -83,22 +83,23 @@ private static LfBranchImpl createTransformer(TwoWindingsTransformer twt, LfNetw
// we don't have any phase or voltage control, we create a simple pi model (single tap) based on phase current
// tap and voltage current tap
Transformers.TapCharacteristics tapCharacteristics = Transformers.getTapCharacteristics(twt);
piModel = Transformers.createPiModel(tapCharacteristics, zb, baseRatio, twtSplitShuntAdmittance);
piModel = Transformers.createPiModel(tapCharacteristics, zb, baseRatio, parameters.isTwtSplitShuntAdmittance());
}

return new LfBranchImpl(network, bus1, bus2, piModel, twt, dc, lowImpedanceThreshold);
return new LfBranchImpl(network, bus1, bus2, piModel, twt, parameters);
}

public static LfBranchImpl create(Branch<?> branch, LfNetwork network, LfBus bus1, LfBus bus2, boolean twtSplitShuntAdmittance,
boolean dc, double lowImpedanceThreshold) {
public static LfBranchImpl create(Branch<?> branch, LfNetwork network, LfBus bus1, LfBus bus2, LfNetworkParameters parameters) {
Objects.requireNonNull(branch);
Objects.requireNonNull(network);
Objects.requireNonNull(parameters);
double nominalV2 = branch.getTerminal2().getVoltageLevel().getNominalV();
double zb = PerUnit.zb(nominalV2);
if (branch instanceof Line) {
return createLine((Line) branch, network, bus1, bus2, zb, dc, lowImpedanceThreshold);
return createLine((Line) branch, network, bus1, bus2, zb, parameters);
} else if (branch instanceof TwoWindingsTransformer) {
TwoWindingsTransformer twt = (TwoWindingsTransformer) branch;
return createTransformer(twt, network, bus1, bus2, zb, twtSplitShuntAdmittance, dc, lowImpedanceThreshold);
return createTransformer(twt, network, bus1, bus2, zb, parameters);
} else {
throw new PowsyblException("Unsupported type of branch for flow equations of branch: " + branch.getId());
}
Expand Down
Loading

0 comments on commit 7329c91

Please sign in to comment.