From c311f5295a5c832fd4020bfd78792138b61c65ea Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Mon, 30 Dec 2024 10:32:04 +0100 Subject: [PATCH] Fix performance issue when accessing loading limits from IIDM Signed-off-by: Geoffroy Jamgotchian --- .../network/impl/AbstractLfBranch.java | 19 +++++++++++++------ .../network/impl/LfBranchImpl.java | 12 ++++++------ .../network/impl/LfDanglingLineBranch.java | 6 +++--- .../network/impl/LfLegBranch.java | 6 +++--- .../network/impl/LfTieLineBranch.java | 14 +++++++------- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBranch.java b/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBranch.java index ad12d90ec3..b0dbc09771 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/AbstractLfBranch.java @@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory; import java.util.*; +import java.util.function.Supplier; /** * @author Geoffroy Jamgotchian {@literal } @@ -132,16 +133,22 @@ public LfBus getBus2() { return bus2; } - public List getLimits1(LimitType type, LoadingLimits loadingLimits, LimitReductionManager limitReductionManager) { + public List getLimits1(LimitType type, Supplier> loadingLimitsSupplier, LimitReductionManager limitReductionManager) { // It is possible to apply the reductions here since the only supported ContingencyContext for LimitReduction is ALL. - return limits1.computeIfAbsent(type, v -> createSortedLimitsList(loadingLimits, bus1, - getLimitReductions(TwoSides.ONE, limitReductionManager, loadingLimits))); + return limits1.computeIfAbsent(type, v -> { + var loadingLimits = loadingLimitsSupplier.get().orElse(null); + return createSortedLimitsList(loadingLimits, bus1, + getLimitReductions(TwoSides.ONE, limitReductionManager, loadingLimits)); + }); } - public List getLimits2(LimitType type, LoadingLimits loadingLimits, LimitReductionManager limitReductionManager) { + public List getLimits2(LimitType type, Supplier> loadingLimitsSupplier, LimitReductionManager limitReductionManager) { // It is possible to apply the reductions here since the only supported ContingencyContext for LimitReduction is ALL. - return limits2.computeIfAbsent(type, v -> createSortedLimitsList(loadingLimits, bus2, - getLimitReductions(TwoSides.TWO, limitReductionManager, loadingLimits))); + return limits2.computeIfAbsent(type, v -> { + var loadingLimits = loadingLimitsSupplier.get().orElse(null); + return createSortedLimitsList(loadingLimits, bus2, + getLimitReductions(TwoSides.TWO, limitReductionManager, loadingLimits)); + }); } @Override 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 02b32c8cd5..4425ea80f1 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfBranchImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfBranchImpl.java @@ -237,11 +237,11 @@ public List getLimits1(final LimitType type, LimitReductionManager limi var branch = getBranch(); switch (type) { case ACTIVE_POWER: - return getLimits1(type, branch.getActivePowerLimits1().orElse(null), limitReductionManager); + return getLimits1(type, branch::getActivePowerLimits1, limitReductionManager); case APPARENT_POWER: - return getLimits1(type, branch.getApparentPowerLimits1().orElse(null), limitReductionManager); + return getLimits1(type, branch::getApparentPowerLimits1, limitReductionManager); case CURRENT: - return getLimits1(type, branch.getCurrentLimits1().orElse(null), limitReductionManager); + return getLimits1(type, branch::getCurrentLimits1, limitReductionManager); case VOLTAGE: default: throw new UnsupportedOperationException(String.format("Getting %s limits is not supported.", type.name())); @@ -253,11 +253,11 @@ public List getLimits2(final LimitType type, LimitReductionManager limi var branch = getBranch(); switch (type) { case ACTIVE_POWER: - return getLimits2(type, branch.getActivePowerLimits2().orElse(null), limitReductionManager); + return getLimits2(type, branch::getActivePowerLimits2, limitReductionManager); case APPARENT_POWER: - return getLimits2(type, branch.getApparentPowerLimits2().orElse(null), limitReductionManager); + return getLimits2(type, branch::getApparentPowerLimits2, limitReductionManager); case CURRENT: - return getLimits2(type, branch.getCurrentLimits2().orElse(null), limitReductionManager); + return getLimits2(type, branch::getCurrentLimits2, limitReductionManager); case VOLTAGE: default: throw new UnsupportedOperationException(String.format("Getting %s limits is not supported.", type.name())); 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 b00cc7eb92..81eb56b5d6 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfDanglingLineBranch.java @@ -83,11 +83,11 @@ public List getLimits1(final LimitType type, LimitReductionManager limi var danglingLine = getDanglingLine(); switch (type) { case ACTIVE_POWER: - return getLimits1(type, danglingLine.getActivePowerLimits().orElse(null), limitReductionManager); + return getLimits1(type, danglingLine::getActivePowerLimits, limitReductionManager); case APPARENT_POWER: - return getLimits1(type, danglingLine.getApparentPowerLimits().orElse(null), limitReductionManager); + return getLimits1(type, danglingLine::getApparentPowerLimits, limitReductionManager); case CURRENT: - return getLimits1(type, danglingLine.getCurrentLimits().orElse(null), limitReductionManager); + return getLimits1(type, danglingLine::getCurrentLimits, limitReductionManager); case VOLTAGE: default: throw new UnsupportedOperationException(String.format("Getting %s limits is not supported.", type.name())); 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 a11d2226d6..0676cbc3d1 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfLegBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfLegBranch.java @@ -150,11 +150,11 @@ public List getLimits1(final LimitType type, LimitReductionManager limi var leg = getLeg(); switch (type) { case ACTIVE_POWER: - return getLimits1(type, leg.getActivePowerLimits().orElse(null), limitReductionManager); + return getLimits1(type, leg::getActivePowerLimits, limitReductionManager); case APPARENT_POWER: - return getLimits1(type, leg.getApparentPowerLimits().orElse(null), limitReductionManager); + return getLimits1(type, leg::getApparentPowerLimits, limitReductionManager); case CURRENT: - return getLimits1(type, leg.getCurrentLimits().orElse(null), limitReductionManager); + return getLimits1(type, leg::getCurrentLimits, limitReductionManager); case VOLTAGE: default: throw new UnsupportedOperationException(String.format("Getting %s limits is not supported.", type.name())); diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/LfTieLineBranch.java b/src/main/java/com/powsybl/openloadflow/network/impl/LfTieLineBranch.java index 65bbd6cd94..d299143546 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfTieLineBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfTieLineBranch.java @@ -105,11 +105,11 @@ public List createBranchResult(double preContingencyBranchP1, doub public List getLimits1(final LimitType type, LimitReductionManager limitReductionManager) { switch (type) { case ACTIVE_POWER: - return getLimits1(type, getHalf1().getActivePowerLimits().orElse(null), limitReductionManager); + return getLimits1(type, getHalf1()::getActivePowerLimits, limitReductionManager); case APPARENT_POWER: - return getLimits1(type, getHalf1().getApparentPowerLimits().orElse(null), limitReductionManager); + return getLimits1(type, getHalf1()::getApparentPowerLimits, limitReductionManager); case CURRENT: - return getLimits1(type, getHalf1().getCurrentLimits().orElse(null), limitReductionManager); + return getLimits1(type, getHalf1()::getCurrentLimits, limitReductionManager); case VOLTAGE: default: throw new UnsupportedOperationException(String.format("Getting %s limits is not supported.", type.name())); @@ -120,11 +120,11 @@ public List getLimits1(final LimitType type, LimitReductionManager limi public List getLimits2(final LimitType type, LimitReductionManager limitReductionManager) { switch (type) { case ACTIVE_POWER: - return getLimits2(type, getHalf2().getActivePowerLimits().orElse(null), limitReductionManager); - case APPARENT_POWER: - return getLimits2(type, getHalf2().getApparentPowerLimits().orElse(null), limitReductionManager); + return getLimits2(type, getHalf2()::getActivePowerLimits, limitReductionManager); case CURRENT: - return getLimits2(type, getHalf2().getCurrentLimits().orElse(null), limitReductionManager); + return getLimits2(type, getHalf2()::getCurrentLimits, limitReductionManager); + case APPARENT_POWER: + return getLimits2(type, getHalf2()::getApparentPowerLimits, limitReductionManager); case VOLTAGE: default: throw new UnsupportedOperationException(String.format("Getting %s limits is not supported.", type.name()));