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

Fix weak refs with network store IIDM #727

Merged
merged 5 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public class OpenLoadFlowParameters extends AbstractExtension<LoadFlowParameters

public static final double DC_POWER_FACTOR_DEFAULT_VALUE = 1.0;

private static final boolean NETWORK_CACHE_ENABLED_DEFAULT_VALUE = false;

public static final boolean SVC_VOLTAGE_MONITORING_DEFAULT_VALUE = true;

public static final VoltageInitModeOverride VOLTAGE_INIT_MODE_OVERRIDE_DEFAULT_VALUE = VoltageInitModeOverride.NONE;
Expand Down Expand Up @@ -164,7 +162,7 @@ private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> e
new Parameter(MAX_REALISTIC_VOLTAGE_NAME, ParameterType.DOUBLE, "Max realistic voltage", NewtonRaphsonParameters.DEFAULT_MAX_REALISTIC_VOLTAGE),
new Parameter(REACTIVE_RANGE_CHECK_MODE_NAME, ParameterType.STRING, "Reactive range check mode", LfNetworkParameters.REACTIVE_RANGE_CHECK_MODE_DEFAULT_VALUE.name(), getEnumPossibleValues(ReactiveRangeCheckMode.class)),
new Parameter(LOW_IMPEDANCE_THRESHOLD_NAME, ParameterType.DOUBLE, "Low impedance threshold in per unit", LfNetworkParameters.LOW_IMPEDANCE_THRESHOLD_DEFAULT_VALUE),
new Parameter(NETWORK_CACHE_ENABLED_NAME, ParameterType.BOOLEAN, "Network cache enabled", NETWORK_CACHE_ENABLED_DEFAULT_VALUE),
new Parameter(NETWORK_CACHE_ENABLED_NAME, ParameterType.BOOLEAN, "Network cache enabled", LfNetworkParameters.CACHE_ENABLED_DEFAULT_VALUE),
new Parameter(SVC_VOLTAGE_MONITORING_NAME, ParameterType.BOOLEAN, "SVC voltage monitoring", SVC_VOLTAGE_MONITORING_DEFAULT_VALUE),
new Parameter(STATE_VECTOR_SCALING_MODE_NAME, ParameterType.STRING, "State vector scaling mode", NewtonRaphsonParameters.DEFAULT_STATE_VECTOR_SCALING_MODE.name(), getEnumPossibleValues(StateVectorScalingMode.class)),
new Parameter(MAX_SLACK_BUS_COUNT_NAME, ParameterType.INTEGER, "Maximum slack buses count", LfNetworkParameters.DEFAULT_MAX_SLACK_BUS_COUNT),
Expand Down Expand Up @@ -246,7 +244,7 @@ public enum ReactiveRangeCheckMode {

private ReactiveRangeCheckMode reactiveRangeCheckMode = LfNetworkParameters.REACTIVE_RANGE_CHECK_MODE_DEFAULT_VALUE;

private boolean networkCacheEnabled = NETWORK_CACHE_ENABLED_DEFAULT_VALUE;
private boolean networkCacheEnabled = LfNetworkParameters.CACHE_ENABLED_DEFAULT_VALUE;

private boolean svcVoltageMonitoring = SVC_VOLTAGE_MONITORING_DEFAULT_VALUE;

Expand Down Expand Up @@ -586,9 +584,9 @@ public static OpenLoadFlowParameters load(PlatformConfig platformConfig) {
.setMaxRealisticVoltage(config.getDoubleProperty(MAX_REALISTIC_VOLTAGE_NAME, NewtonRaphsonParameters.DEFAULT_MAX_REALISTIC_VOLTAGE))
.setReactiveRangeCheckMode(config.getEnumProperty(REACTIVE_RANGE_CHECK_MODE_NAME, ReactiveRangeCheckMode.class, LfNetworkParameters.REACTIVE_RANGE_CHECK_MODE_DEFAULT_VALUE))
.setLowImpedanceThreshold(config.getDoubleProperty(LOW_IMPEDANCE_THRESHOLD_NAME, LfNetworkParameters.LOW_IMPEDANCE_THRESHOLD_DEFAULT_VALUE))
.setNetworkCacheEnabled(config.getBooleanProperty(NETWORK_CACHE_ENABLED_NAME, NETWORK_CACHE_ENABLED_DEFAULT_VALUE))
.setNetworkCacheEnabled(config.getBooleanProperty(NETWORK_CACHE_ENABLED_NAME, LfNetworkParameters.CACHE_ENABLED_DEFAULT_VALUE))
.setSvcVoltageMonitoring(config.getBooleanProperty(SVC_VOLTAGE_MONITORING_NAME, SVC_VOLTAGE_MONITORING_DEFAULT_VALUE))
.setNetworkCacheEnabled(config.getBooleanProperty(NETWORK_CACHE_ENABLED_NAME, NETWORK_CACHE_ENABLED_DEFAULT_VALUE))
.setNetworkCacheEnabled(config.getBooleanProperty(NETWORK_CACHE_ENABLED_NAME, LfNetworkParameters.CACHE_ENABLED_DEFAULT_VALUE))
.setStateVectorScalingMode(config.getEnumProperty(STATE_VECTOR_SCALING_MODE_NAME, StateVectorScalingMode.class, NewtonRaphsonParameters.DEFAULT_STATE_VECTOR_SCALING_MODE))
.setMaxSlackBusCount(config.getIntProperty(MAX_SLACK_BUS_COUNT_NAME, LfNetworkParameters.DEFAULT_MAX_SLACK_BUS_COUNT))
.setDebugDir(config.getStringProperty(DEBUG_DIR_PARAM_NAME, LfNetworkParameters.DEBUG_DIR_DEFAULT_VALUE))
Expand Down Expand Up @@ -839,7 +837,8 @@ static LfNetworkParameters getNetworkParameters(LoadFlowParameters parameters, O
.setSvcVoltageMonitoring(parametersExt.isSvcVoltageMonitoring())
.setMaxSlackBusCount(parametersExt.getMaxSlackBusCount())
.setDebugDir(parametersExt.getDebugDir())
.setSecondaryVoltageControl(parametersExt.isSecondaryVoltageControl());
.setSecondaryVoltageControl(parametersExt.isSecondaryVoltageControl())
.setCacheEnabled(parametersExt.isNetworkCacheEnabled());
}

public static AcLoadFlowParameters createAcParameters(Network network, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ private LoadFlowResult runDc(Network network, LoadFlowParameters parameters, Rep
OpenLoadFlowParameters.logDc(parameters, parametersExt);

var dcParameters = OpenLoadFlowParameters.createDcParameters(network, parameters, parametersExt, matrixFactory, connectivityFactory, forcePhaseControlOffAndAddAngle1Var);
dcParameters.getNetworkParameters()
.setCacheEnabled(false); // force not caching as not supported in DC LF

List<DcLoadFlowResult> results = DcLoadFlowEngine.run(network, new LfNetworkLoaderImpl(), dcParameters, reporter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class LfNetworkParameters {

public static final boolean SECONDARY_VOLTAGE_CONTROL_DEFAULT_VALUE = false;

public static final boolean CACHE_ENABLED_DEFAULT_VALUE = false;

private SlackBusSelector slackBusSelector = new FirstSlackBusSelector();

private GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory = new EvenShiloachGraphDecrementalConnectivityFactory<>();
Expand Down Expand Up @@ -94,6 +96,8 @@ public class LfNetworkParameters {

private boolean secondaryVoltageControl = SECONDARY_VOLTAGE_CONTROL_DEFAULT_VALUE;

private boolean cacheEnabled = CACHE_ENABLED_DEFAULT_VALUE;

public SlackBusSelector getSlackBusSelector() {
return slackBusSelector;
}
Expand Down Expand Up @@ -347,6 +351,15 @@ public LfNetworkParameters setSecondaryVoltageControl(boolean secondaryVoltageCo
return this;
}

public boolean isCacheEnabled() {
return cacheEnabled;
}

public LfNetworkParameters setCacheEnabled(boolean cacheEnabled) {
this.cacheEnabled = cacheEnabled;
return this;
}

@Override
public String toString() {
return "LfNetworkParameters(" +
Expand Down Expand Up @@ -376,6 +389,7 @@ public String toString() {
", maxSlackBusCount=" + maxSlackBusCount +
", debugDir=" + debugDir +
", secondaryVoltageControl=" + secondaryVoltageControl +
", cacheEnabled=" + cacheEnabled +
')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,20 @@ public void setVoltageControlEnabled(boolean voltageControlEnabled) {
}
}

void addLoad(Load load) {
void addLoad(Load load, LfNetworkParameters parameters) {
double p0 = load.getP0();
loadTargetP += p0;
initialLoadTargetP += p0;
loadTargetQ += load.getQ0();
if (p0 < 0) {
ensurePowerFactorConstantByLoad = true;
}
lfAggregatedLoads.add(load);
lfAggregatedLoads.add(load, parameters);
}

void addLccConverterStation(LccConverterStation lccCs) {
void addLccConverterStation(LccConverterStation lccCs, LfNetworkParameters parameters) {
// note that LCC converter station are out of the slack distribution.
lccCsRefs.add(new Ref<>(lccCs));
lccCsRefs.add(Ref.create(lccCs, parameters.isCacheEnabled()));
double targetP = HvdcConverterStations.getConverterStationTargetP(lccCs);
loadTargetP += targetP;
initialLoadTargetP += targetP;
Expand Down Expand Up @@ -253,19 +253,19 @@ void addBattery(Battery generator, LfNetworkParameters parameters, LfNetworkLoad

void setShuntCompensators(List<ShuntCompensator> shuntCompensators, LfNetworkParameters parameters) {
if (!parameters.isShuntVoltageControl() && !shuntCompensators.isEmpty()) {
shunt = new LfShuntImpl(shuntCompensators, network, this, false);
shunt = new LfShuntImpl(shuntCompensators, network, this, false, parameters);
} else {
List<ShuntCompensator> controllerShuntCompensators = shuntCompensators.stream()
.filter(ShuntCompensator::isVoltageRegulatorOn)
.collect(Collectors.toList());
if (!controllerShuntCompensators.isEmpty()) {
controllerShunt = new LfShuntImpl(controllerShuntCompensators, network, this, true);
controllerShunt = new LfShuntImpl(controllerShuntCompensators, network, this, true, parameters);
}
List<ShuntCompensator> fixedShuntCompensators = shuntCompensators.stream()
.filter(sc -> !sc.isVoltageRegulatorOn())
.collect(Collectors.toList());
if (!fixedShuntCompensators.isEmpty()) {
shunt = new LfShuntImpl(fixedShuntCompensators, network, this, false);
shunt = new LfShuntImpl(fixedShuntCompensators, network, this, false, parameters);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.iidm.network.extensions.LoadDetail;
import com.powsybl.openloadflow.network.AbstractPropertyBag;
import com.powsybl.openloadflow.network.LfAggregatedLoads;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.util.PerUnit;

import java.util.*;
Expand Down Expand Up @@ -41,8 +42,8 @@ public List<String> getOriginalIds() {
return loadsRefs.stream().map(r -> r.get().getId()).collect(Collectors.toList());
}

void add(Load load) {
loadsRefs.add(new Ref<>(load));
void add(Load load, LfNetworkParameters parameters) {
loadsRefs.add(Ref.create(load, parameters.isCacheEnabled()));
loadsStatus.put(load.getId(), false);
initialized = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class LfBatteryImpl extends AbstractLfGenerator {

private LfBatteryImpl(Battery battery, LfNetwork network, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
super(network, battery.getTargetP());
this.batteryRef = new Ref<>(battery);
this.batteryRef = Ref.create(battery, parameters.isCacheEnabled());
participating = true;
droop = DEFAULT_DROOP;
// get participation factor from extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class LfBranchImpl extends AbstractImpedantLfBranch {

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);
this.branchRef = Ref.create(branch, parameters.isCacheEnabled());
}

private static LfBranchImpl createLine(Line line, LfNetwork network, LfBus bus1, LfBus bus2, double zb, LfNetworkParameters parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class LfBusImpl extends AbstractLfBus {
protected LfBusImpl(Bus bus, LfNetwork network, double v, double angle, LfNetworkParameters parameters,
boolean participating) {
super(network, v, angle, parameters.isDistributedOnConformLoad());
this.busRef = new Ref<>(bus);
this.busRef = Ref.create(bus, parameters.isCacheEnabled());
nominalV = bus.getVoltageLevel().getNominalV();
lowVoltageLimit = bus.getVoltageLevel().getLowVoltageLimit();
highVoltageLimit = bus.getVoltageLevel().getHighVoltageLimit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LfDanglingLineBranch extends AbstractImpedantLfBranch {
protected LfDanglingLineBranch(LfNetwork network, LfBus bus1, LfBus bus2, PiModel piModel, DanglingLine danglingLine,
LfNetworkParameters parameters) {
super(network, bus1, bus2, piModel, parameters);
this.danglingLineRef = new Ref<>(danglingLine);
this.danglingLineRef = Ref.create(danglingLine, parameters.isCacheEnabled());
}

public static LfDanglingLineBranch create(DanglingLine danglingLine, LfNetwork network, LfBus bus1, LfBus bus2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LfDanglingLineBus extends AbstractLfBus {

public LfDanglingLineBus(LfNetwork network, DanglingLine danglingLine, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
super(network, Networks.getPropertyV(danglingLine), Networks.getPropertyAngle(danglingLine), false);
this.danglingLineRef = new Ref<>(danglingLine);
this.danglingLineRef = Ref.create(danglingLine, parameters.isCacheEnabled());
nominalV = danglingLine.getTerminal().getVoltageLevel().getNominalV();
loadTargetP += danglingLine.getP0();
loadTargetQ += danglingLine.getQ0();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class LfDanglingLineGenerator extends AbstractLfGenerator {
private LfDanglingLineGenerator(DanglingLine danglingLine, LfNetwork network, String controlledLfBusId, LfNetworkParameters parameters,
LfNetworkLoadingReport report) {
super(network, danglingLine.getGeneration().getTargetP());
this.danglingLineRef = new Ref<>(danglingLine);
this.danglingLineRef = Ref.create(danglingLine, parameters.isCacheEnabled());

// local control only
if (danglingLine.getGeneration().isVoltageRegulationOn() && checkVoltageControlConsistency(parameters, report)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class LfGeneratorImpl extends AbstractLfGenerator {

private LfGeneratorImpl(Generator generator, LfNetwork network, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
super(network, generator.getTargetP());
this.generatorRef = new Ref<>(generator);
this.generatorRef = Ref.create(generator, parameters.isCacheEnabled());
participating = true;
droop = DEFAULT_DROOP;
// get participation factor and droop from extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public final class LfLegBranch extends AbstractImpedantLfBranch {
private LfLegBranch(LfNetwork network, LfBus bus1, LfBus bus0, PiModel piModel, ThreeWindingsTransformer twt, ThreeWindingsTransformer.Leg leg,
LfNetworkParameters parameters) {
super(network, bus1, bus0, piModel, parameters);
this.twtRef = new Ref<>(twt);
this.legRef = new Ref<>(leg);
this.twtRef = Ref.create(twt, parameters.isCacheEnabled());
this.legRef = Ref.create(leg, parameters.isCacheEnabled());
}

private ThreeWindingsTransformer getTwt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void visitGenerator(Generator generator) {

@Override
public void visitLoad(Load load) {
lfBus.addLoad(load);
lfBus.addLoad(load, parameters);
postProcessors.forEach(pp -> pp.onInjectionAdded(load, lfBus));
}

Expand Down Expand Up @@ -320,7 +320,7 @@ public void visitHvdcConverterStation(HvdcConverterStation<?> converterStation)
loadingContext.hvdcLineSet.add(converterStation.getHvdcLine());
break;
case LCC:
lfBus.addLccConverterStation((LccConverterStation) converterStation);
lfBus.addLccConverterStation((LccConverterStation) converterStation, parameters);
loadingContext.hvdcLineSet.add(converterStation.getHvdcLine());
break;
default:
Expand Down Expand Up @@ -375,7 +375,7 @@ private static void createBranches(List<LfBus> lfBuses, LfNetwork lfNetwork, Loa
}

for (ThreeWindingsTransformer t3wt : loadingContext.t3wtSet) {
LfStarBus lfBus0 = new LfStarBus(lfNetwork, t3wt);
LfStarBus lfBus0 = new LfStarBus(lfNetwork, t3wt, parameters);
lfNetwork.addBus(lfBus0);
LfBus lfBus1 = getLfBus(t3wt.getLeg1().getTerminal(), lfNetwork, parameters.isBreakers());
LfBus lfBus2 = getLfBus(t3wt.getLeg2().getTerminal(), lfNetwork, parameters.isBreakers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ public Optional<Direction> updateSectionB(double deltaB, int maxSectionShift, Al

private double g;

public LfShuntImpl(List<ShuntCompensator> shuntCompensators, LfNetwork network, LfBus bus, boolean voltageControlCapability) {
public LfShuntImpl(List<ShuntCompensator> shuntCompensators, LfNetwork network, LfBus bus, boolean voltageControlCapability,
LfNetworkParameters parameters) {
// if withVoltageControl equals to true, all shunt compensators that are listed must control voltage.
// if withVoltageControl equals to false, all shunt compensators that are listed will be treated as fixed shunt
// compensators.
super(network);
shuntCompensatorsRefs = Objects.requireNonNull(shuntCompensators).stream().map(Ref::new).collect(Collectors.toList());
shuntCompensatorsRefs = Objects.requireNonNull(shuntCompensators).stream()
.map(sc -> Ref.create(sc, parameters.isCacheEnabled()))
.collect(Collectors.toList());
if (shuntCompensators.isEmpty()) {
throw new IllegalArgumentException("Empty shunt compensator list");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.powsybl.iidm.network.ThreeWindingsTransformer;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.network.LfNetworkStateUpdateParameters;

import java.util.List;
Expand All @@ -21,9 +22,9 @@ public class LfStarBus extends AbstractLfBus {

private final double nominalV;

public LfStarBus(LfNetwork network, ThreeWindingsTransformer t3wt) {
public LfStarBus(LfNetwork network, ThreeWindingsTransformer t3wt, LfNetworkParameters parameters) {
super(network, Networks.getPropertyV(t3wt), Networks.getPropertyAngle(t3wt), false);
this.t3wtRef = new Ref<>(t3wt);
this.t3wtRef = Ref.create(t3wt, parameters.isCacheEnabled());
nominalV = t3wt.getRatedU0();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class LfStaticVarCompensatorImpl extends AbstractLfGenerator implem
private LfStaticVarCompensatorImpl(StaticVarCompensator svc, LfNetwork network, AbstractLfBus bus, LfNetworkParameters parameters,
LfNetworkLoadingReport report) {
super(network, 0);
this.svcRef = new Ref<>(svc);
this.svcRef = Ref.create(svc, parameters.isCacheEnabled());
this.nominalV = svc.getTerminal().getVoltageLevel().getNominalV();
this.reactiveLimits = new MinMaxReactiveLimits() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LfSwitch extends AbstractLfBranch {

public LfSwitch(LfNetwork network, LfBus bus1, LfBus bus2, Switch aSwitch, LfNetworkParameters parameters) {
super(network, bus1, bus2, new SimplePiModel(), parameters);
this.switchRef = new Ref<>(aSwitch);
this.switchRef = Ref.create(aSwitch, parameters.isCacheEnabled());
}

private Switch getSwitch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LfVscConverterStationImpl extends AbstractLfGenerator implements Lf

public LfVscConverterStationImpl(VscConverterStation station, LfNetwork network, LfNetworkParameters parameters, LfNetworkLoadingReport report) {
super(network, HvdcConverterStations.getConverterStationTargetP(station));
this.stationRef = new Ref<>(station);
this.stationRef = Ref.create(station, parameters.isCacheEnabled());
this.lossFactor = station.getLossFactor();

// local control only
Expand Down
Loading