diff --git a/src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java b/src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java index 4d13b10fbe..1cb6b55078 100644 --- a/src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java +++ b/src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java @@ -67,8 +67,6 @@ public class OpenLoadFlowParameters extends AbstractExtension> List getEnumPossibleValues(Class 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), @@ -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; @@ -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)) @@ -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, diff --git a/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java b/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java index be2bab7466..fc5ec35d7d 100644 --- a/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java +++ b/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java @@ -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 results = DcLoadFlowEngine.run(network, new LfNetworkLoaderImpl(), dcParameters, reporter); diff --git a/src/main/java/com/powsybl/openloadflow/network/LfNetworkParameters.java b/src/main/java/com/powsybl/openloadflow/network/LfNetworkParameters.java index 3ef3c8853d..b882f336cb 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfNetworkParameters.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfNetworkParameters.java @@ -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 connectivityFactory = new EvenShiloachGraphDecrementalConnectivityFactory<>(); @@ -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; } @@ -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(" + @@ -376,6 +389,7 @@ public String toString() { ", maxSlackBusCount=" + maxSlackBusCount + ", debugDir=" + debugDir + ", secondaryVoltageControl=" + secondaryVoltageControl + + ", cacheEnabled=" + cacheEnabled + ')'; } } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBus.java b/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBus.java index 3990524b7c..e14cf2e7d2 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBus.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBus.java @@ -197,7 +197,7 @@ public void setVoltageControlEnabled(boolean voltageControlEnabled) { } } - void addLoad(Load load) { + void addLoad(Load load, LfNetworkParameters parameters) { double p0 = load.getP0(); loadTargetP += p0; initialLoadTargetP += p0; @@ -205,12 +205,12 @@ void addLoad(Load load) { 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; @@ -253,19 +253,19 @@ void addBattery(Battery generator, LfNetworkParameters parameters, LfNetworkLoad void setShuntCompensators(List 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 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 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); } } } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfAggregatedLoadsImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfAggregatedLoadsImpl.java index 8e689706b0..58f57ad728 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfAggregatedLoadsImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfAggregatedLoadsImpl.java @@ -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.*; @@ -41,8 +42,8 @@ public List 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; } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfBatteryImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfBatteryImpl.java index a96bc110d6..93a6e2cf9a 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfBatteryImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfBatteryImpl.java @@ -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 diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfBranchImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfBranchImpl.java index 8bb8b198ec..ae6df4e58e 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfBranchImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfBranchImpl.java @@ -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) { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfBusImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfBusImpl.java index 8c4fd76754..c2c59b4495 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfBusImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfBusImpl.java @@ -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(); diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBranch.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBranch.java index eeab9113fb..7fe998e216 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBranch.java @@ -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, diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBus.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBus.java index ec0f154418..bd9ad5a9ab 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBus.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBus.java @@ -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(); diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineGenerator.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineGenerator.java index 3efdf0c335..25e2b043ee 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineGenerator.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineGenerator.java @@ -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)) { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfGeneratorImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfGeneratorImpl.java index 62c420fdcb..07e1f5ab61 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfGeneratorImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfGeneratorImpl.java @@ -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 diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfLegBranch.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfLegBranch.java index a4a2c7ac29..34d44e7f52 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfLegBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfLegBranch.java @@ -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() { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfNetworkLoaderImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfNetworkLoaderImpl.java index 90e038633b..0266b504ae 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfNetworkLoaderImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfNetworkLoaderImpl.java @@ -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)); } @@ -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: @@ -375,7 +375,7 @@ private static void createBranches(List 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()); diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java index b77772cb2c..ab07a84fcf 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java @@ -58,12 +58,15 @@ public Optional updateSectionB(double deltaB, int maxSectionShift, Al private double g; - public LfShuntImpl(List shuntCompensators, LfNetwork network, LfBus bus, boolean voltageControlCapability) { + public LfShuntImpl(List 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"); } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfStarBus.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfStarBus.java index 97f4308a4f..913d929de3 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfStarBus.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfStarBus.java @@ -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; @@ -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(); } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfStaticVarCompensatorImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfStaticVarCompensatorImpl.java index 1776d88253..852cd873fa 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfStaticVarCompensatorImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfStaticVarCompensatorImpl.java @@ -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() { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfSwitch.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfSwitch.java index 5245c52df0..5fbd95490a 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfSwitch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfSwitch.java @@ -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() { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfVscConverterStationImpl.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfVscConverterStationImpl.java index cdf2271d54..5a2a9e7011 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfVscConverterStationImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfVscConverterStationImpl.java @@ -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 diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/Ref.java b/src/main/java/com/powsybl/openloadflow/network/impl/Ref.java index 53092c8944..17c27345c9 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/Ref.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/Ref.java @@ -6,21 +6,14 @@ */ package com.powsybl.openloadflow.network.impl; -import java.lang.ref.WeakReference; -import java.util.Objects; - /** * @author Geoffroy Jamgotchian */ -public class Ref { - - private final WeakReference value; +public interface Ref { - public Ref(T identifiable) { - this.value = new WeakReference<>(Objects.requireNonNull(identifiable)); - } + T get(); - public T get() { - return Objects.requireNonNull(value.get(), "Reference has been garbage collected"); + static Ref create(T identifiable, boolean cacheEnabled) { + return cacheEnabled ? new WeakRef<>(identifiable) : new StrongRef<>(identifiable); } } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/StrongRef.java b/src/main/java/com/powsybl/openloadflow/network/impl/StrongRef.java new file mode 100644 index 0000000000..952375ef9a --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/network/impl/StrongRef.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.openloadflow.network.impl; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian + */ +class StrongRef implements Ref { + + private final T value; + + StrongRef(T identifiable) { + this.value = Objects.requireNonNull(identifiable); + } + + @Override + public T get() { + return value; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/WeakRef.java b/src/main/java/com/powsybl/openloadflow/network/impl/WeakRef.java new file mode 100644 index 0000000000..729df5eb42 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/network/impl/WeakRef.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.powsybl.openloadflow.network.impl; + +import java.lang.ref.WeakReference; +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian + */ +class WeakRef implements Ref { + + private final WeakReference value; + + WeakRef(T identifiable) { + this.value = new WeakReference<>(Objects.requireNonNull(identifiable)); + } + + @Override + public T get() { + return Objects.requireNonNull(value.get(), "Reference has been garbage collected"); + } +} diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index 7d9473ca4e..f15cc9ca1a 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -90,6 +90,8 @@ SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParamete findAllSwitchesToOperate(network, actions, allSwitchesToClose, allSwitchesToOpen); boolean breakers = !(allSwitchesToOpen.isEmpty() && allSwitchesToClose.isEmpty()); AcLoadFlowParameters acParameters = OpenLoadFlowParameters.createAcParameters(network, lfParameters, lfParametersExt, matrixFactory, connectivityFactory, breakers, false); + acParameters.getNetworkParameters() + .setCacheEnabled(false); // force not caching as not supported in secu analysis // create networks including all necessary switches try (LfNetworkList lfNetworks = Networks.load(network, acParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, saReporter)) { diff --git a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java index 90c384b881..3e4c5fee43 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java @@ -244,7 +244,9 @@ private List createOperatorStrategyResults(DcSecurityAna var dcParameters = OpenLoadFlowParameters.createDcParameters(network, context.getParameters().getLoadFlowParameters(), parametersExt, matrixFactory, connectivityFactory, false); - dcParameters.getNetworkParameters().setBreakers(breakers); + dcParameters.getNetworkParameters() + .setBreakers(breakers) + .setCacheEnabled(false); // force not caching as not supported in secu analysis try (LfNetworkList lfNetworks = Networks.load(network, dcParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, Reporter.NO_OP)) { return lfNetworks.getLargest().filter(LfNetwork::isValid) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java index d20e57b34a..cf80065daf 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java @@ -230,7 +230,8 @@ public void analyse(Network network, List contingencies, .setReactiveLimits(lfParameters.isUseReactiveLimits()) .setHvdcAcEmulation(lfParameters.isHvdcAcEmulation()) .setMinPlausibleTargetVoltage(lfParametersExt.getMinPlausibleTargetVoltage()) - .setMaxPlausibleTargetVoltage(lfParametersExt.getMaxPlausibleTargetVoltage()); + .setMaxPlausibleTargetVoltage(lfParametersExt.getMaxPlausibleTargetVoltage()) + .setCacheEnabled(false); // force not caching as not supported in sensi analysis // create networks including all necessary switches try (LfNetworkList lfNetworks = Networks.load(network, lfNetworkParameters, allSwitchesToOpen, Collections.emptySet(), reporter)) { diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index 4d8d001744..5976e7f453 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -803,7 +803,8 @@ public void analyse(Network network, List contingencies, .setReactiveLimits(false) .setHvdcAcEmulation(false) .setMinPlausibleTargetVoltage(lfParametersExt.getMinPlausibleTargetVoltage()) - .setMaxPlausibleTargetVoltage(lfParametersExt.getMaxPlausibleTargetVoltage()); + .setMaxPlausibleTargetVoltage(lfParametersExt.getMaxPlausibleTargetVoltage()) + .setCacheEnabled(false); // force not caching as not supported in sensi analysis // create networks including all necessary switches try (LfNetworkList lfNetworks = Networks.load(network, lfNetworkParameters, allSwitchesToOpen, Collections.emptySet(), reporter)) { LfNetwork lfNetwork = lfNetworks.getLargest().orElseThrow(() -> new PowsyblException("Empty network")); diff --git a/src/test/java/com/powsybl/openloadflow/OpenLoadFlowProviderTest.java b/src/test/java/com/powsybl/openloadflow/OpenLoadFlowProviderTest.java index b61695d9b9..305e56671e 100644 --- a/src/test/java/com/powsybl/openloadflow/OpenLoadFlowProviderTest.java +++ b/src/test/java/com/powsybl/openloadflow/OpenLoadFlowProviderTest.java @@ -49,7 +49,7 @@ void test() { void testDcParameters() { Network network = Mockito.mock(Network.class); DcLoadFlowParameters dcParameters = OpenLoadFlowParameters.createDcParameters(network, new LoadFlowParameters().setReadSlackBus(true), new OpenLoadFlowParameters(), new DenseMatrixFactory(), new EvenShiloachGraphDecrementalConnectivityFactory<>(), true); - assertEquals("DcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=false, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, dc=true, reactiveLimits=false, hvdcAcEmulation=false, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2, loaderPostProcessorSelection=[], reactiveRangeCheckMode=MAX, lowImpedanceThreshold=1.0E-8, svcVoltageMonitoring=false, maxSlackBusCount=1, debugDir=null, secondaryVoltageControl=false), equationSystemCreationParameters=DcEquationSystemCreationParameters(updateFlows=true, forcePhaseControlOffAndAddAngle1Var=true, useTransformerRatio=true), matrixFactory=DenseMatrixFactory, distributedSlack=true, balanceType=PROPORTIONAL_TO_GENERATION_P_MAX, setVToNan=true)", + assertEquals("DcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=false, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, dc=true, reactiveLimits=false, hvdcAcEmulation=false, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2, loaderPostProcessorSelection=[], reactiveRangeCheckMode=MAX, lowImpedanceThreshold=1.0E-8, svcVoltageMonitoring=false, maxSlackBusCount=1, debugDir=null, secondaryVoltageControl=false, cacheEnabled=false), equationSystemCreationParameters=DcEquationSystemCreationParameters(updateFlows=true, forcePhaseControlOffAndAddAngle1Var=true, useTransformerRatio=true), matrixFactory=DenseMatrixFactory, distributedSlack=true, balanceType=PROPORTIONAL_TO_GENERATION_P_MAX, setVToNan=true)", dcParameters.toString()); } @@ -57,7 +57,7 @@ void testDcParameters() { void testAcParameters() { Network network = Mockito.mock(Network.class); AcLoadFlowParameters acParameters = OpenLoadFlowParameters.createAcParameters(network, new LoadFlowParameters().setReadSlackBus(true), new OpenLoadFlowParameters(), new DenseMatrixFactory(), new EvenShiloachGraphDecrementalConnectivityFactory<>(), false, false); - assertEquals("AcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=true, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, dc=false, reactiveLimits=true, hvdcAcEmulation=true, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2, loaderPostProcessorSelection=[], reactiveRangeCheckMode=MAX, lowImpedanceThreshold=1.0E-8, svcVoltageMonitoring=true, maxSlackBusCount=1, debugDir=null, secondaryVoltageControl=false), equationSystemCreationParameters=AcEquationSystemCreationParameters(forceA1Var=false), newtonRaphsonParameters=NewtonRaphsonParameters(maxIteration=30, minRealisticVoltage=0.5, maxRealisticVoltage=1.5, stoppingCriteria=DefaultNewtonRaphsonStoppingCriteria, stateVectorScalingMode=NONE), outerLoops=[DistributedSlackOuterLoop, MonitoringVoltageOuterLoop, ReactiveLimitsOuterLoop], matrixFactory=DenseMatrixFactory, voltageInitializer=UniformValueVoltageInitializer)", + assertEquals("AcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=true, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, dc=false, reactiveLimits=true, hvdcAcEmulation=true, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2, loaderPostProcessorSelection=[], reactiveRangeCheckMode=MAX, lowImpedanceThreshold=1.0E-8, svcVoltageMonitoring=true, maxSlackBusCount=1, debugDir=null, secondaryVoltageControl=false, cacheEnabled=false), equationSystemCreationParameters=AcEquationSystemCreationParameters(forceA1Var=false), newtonRaphsonParameters=NewtonRaphsonParameters(maxIteration=30, minRealisticVoltage=0.5, maxRealisticVoltage=1.5, stoppingCriteria=DefaultNewtonRaphsonStoppingCriteria, stateVectorScalingMode=NONE), outerLoops=[DistributedSlackOuterLoop, MonitoringVoltageOuterLoop, ReactiveLimitsOuterLoop], matrixFactory=DenseMatrixFactory, voltageInitializer=UniformValueVoltageInitializer)", acParameters.toString()); }