From 400296f61961a6bb4c9ff63822a5d6b4ede9963e Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 14:49:05 +0200 Subject: [PATCH 01/32] AC equations vectorization Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcBranchVector.java | 165 ++++++++ .../ac/equations/AcBusVector.java | 25 ++ .../ac/equations/AcEquationSystemCreator.java | 4 + .../ac/equations/AcNetworkVector.java | 387 ++++++++++++++++++ ...osedBranchSide1ActiveFlowEquationTerm.java | 12 +- ...edBranchSide1ReactiveFlowEquationTerm.java | 12 +- ...osedBranchSide2ActiveFlowEquationTerm.java | 12 +- ...edBranchSide2ReactiveFlowEquationTerm.java | 12 +- ...OpenBranchSide1ActiveFlowEquationTerm.java | 4 +- ...enBranchSide1ReactiveFlowEquationTerm.java | 4 +- ...OpenBranchSide2ActiveFlowEquationTerm.java | 4 +- ...enBranchSide2ReactiveFlowEquationTerm.java | 4 +- 12 files changed, 613 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java new file mode 100644 index 0000000000..797b3cf44b --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java @@ -0,0 +1,165 @@ +/** + * 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.ac.equations; + +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; +import com.powsybl.openloadflow.network.PiModel; +import net.jafama.FastMath; + +import java.util.List; + +/** + * @author Geoffroy Jamgotchian + */ +public class AcBranchVector { + + final int[] bus1Num; + final int[] bus2Num; + + final double[] y; + final double[] ksi; + final double[] cosKsi; + final double[] sinKsi; + final double[] g1; + final double[] g2; + final double[] b1; + final double[] b2; + final double[] r1; + final double[] a1; + + final int[] status; + + final int[] v1Row; + final int[] v2Row; + final int[] ph1Row; + final int[] ph2Row; + + final int[] a1Row; + final int[] r1Row; + + final double[] p1; + final double[] p2; + final double[] q1; + final double[] q2; + final double[] i1; + final double[] i2; + + final double[] dp1dv1; + final double[] dp1dv2; + final double[] dp1dph1; + final double[] dp1dph2; + final double[] dp1da1; + final double[] dp1dr1; + + final double[] dq1dv1; + final double[] dq1dv2; + final double[] dq1dph1; + final double[] dq1dph2; + final double[] dq1da1; + final double[] dq1dr1; + + final double[] dp2dv1; + final double[] dp2dv2; + final double[] dp2dph1; + final double[] dp2dph2; + final double[] dp2da1; + final double[] dp2dr1; + + final double[] dq2dv1; + final double[] dq2dv2; + final double[] dq2dph1; + final double[] dq2dph2; + final double[] dq2da1; + final double[] dq2dr1; + + public AcBranchVector(List branches) { + int size = branches.size(); + bus1Num = new int[size]; + bus2Num = new int[size]; + y = new double[size]; + ksi = new double[size]; + cosKsi = new double[size]; + sinKsi = new double[size]; + g1 = new double[size]; + g2 = new double[size]; + b1 = new double[size]; + b2 = new double[size]; + r1 = new double[size]; + a1 = new double[size]; + status = new int[size]; + + v1Row = new int[size]; + v2Row = new int[size]; + ph1Row = new int[size]; + ph2Row = new int[size]; + a1Row = new int[size]; + r1Row = new int[size]; + + p1 = new double[size]; + p2 = new double[size]; + q1 = new double[size]; + q2 = new double[size]; + i1 = new double[size]; + i2 = new double[size]; + + dp1dv1 = new double[size]; + dp1dv2 = new double[size]; + dp1dph1 = new double[size]; + dp1dph2 = new double[size]; + dp1da1 = new double[size]; + dp1dr1 = new double[size]; + + dq1dv1 = new double[size]; + dq1dv2 = new double[size]; + dq1dph1 = new double[size]; + dq1dph2 = new double[size]; + dq1da1 = new double[size]; + dq1dr1 = new double[size]; + + dp2dv1 = new double[size]; + dp2dv2 = new double[size]; + dp2dph1 = new double[size]; + dp2dph2 = new double[size]; + dp2da1 = new double[size]; + dp2dr1 = new double[size]; + + dq2dv1 = new double[size]; + dq2dv2 = new double[size]; + dq2dph1 = new double[size]; + dq2dph2 = new double[size]; + dq2da1 = new double[size]; + dq2dr1 = new double[size]; + + for (int i = 0; i < branches.size(); i++) { + LfBranch branch = branches.get(i); + LfBus bus1 = branch.getBus1(); + LfBus bus2 = branch.getBus2(); + bus1Num[i] = bus1 != null ? bus1.getNum() : -1; + bus2Num[i] = bus2 != null ? bus2.getNum() : -1; + PiModel piModel = branch.getPiModel(); +// if (piModel.getR() == 0 && piModel.getX() == 0) { +// throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); +// } + y[i] = piModel.getY(); + ksi[i] = piModel.getKsi(); + cosKsi[i] = FastMath.cos(ksi[i]); + sinKsi[i] = FastMath.sin(ksi[i]); + b1[i] = piModel.getB1(); + b2[i] = piModel.getB2(); + g1[i] = piModel.getG1(); + g2[i] = piModel.getG2(); + r1[i] = piModel.getR1(); + a1[i] = piModel.getA1(); + status[i] = branch.isDisabled() ? 0 : 1; + } + } + + public int getSize() { + return status.length; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java new file mode 100644 index 0000000000..7c13362950 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java @@ -0,0 +1,25 @@ +/** + * 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.ac.equations; + +import com.powsybl.openloadflow.network.LfBus; + +import java.util.List; + +/** + * @author Geoffroy Jamgotchian + */ +public class AcBusVector { + + final int[] vRow; + final int[] phRow; + + public AcBusVector(List buses) { + vRow = new int[buses.size()]; + phRow = new int[buses.size()]; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index e0c3ba0367..1753fcdb98 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -760,6 +760,8 @@ public EquationSystem create() { EquationSystem equationSystem = new EquationSystem<>(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem); + createBusesEquations(equationSystem); createMultipleSlackBusesEquations(equationSystem); createBranchesEquations(equationSystem); @@ -774,6 +776,8 @@ public EquationSystem create() { network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(equationSystem, creationParameters))); + networkVector.startListening(); + return equationSystem; } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java new file mode 100644 index 0000000000..e34e420c5c --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -0,0 +1,387 @@ +/** + * 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.ac.equations; + +import com.google.common.base.Stopwatch; +import com.powsybl.openloadflow.equations.*; +import com.powsybl.openloadflow.network.*; +import net.jafama.DoubleWrapper; +import net.jafama.FastMath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * @author Geoffroy Jamgotchian + */ +public class AcNetworkVector extends AbstractLfNetworkListener + implements EquationSystemIndexListener, StateVectorListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(AcNetworkVector.class); + + private static final double SQRT3 = FastMath.sqrt(3); + + private final LfNetwork network; + private final EquationSystem equationSystem; + private final AcBusVector busVector; + private final AcBranchVector branchVector; + + public AcNetworkVector(LfNetwork network, EquationSystem equationSystem) { + this.network = Objects.requireNonNull(network); + this.equationSystem = Objects.requireNonNull(equationSystem); + busVector = new AcBusVector(network.getBuses()); + branchVector = new AcBranchVector(network.getBranches()); + } + + public void startListening() { + // listen for branch disabling status update + network.addListener(this); + + // update vectorized variables and then listen for variables change in equation system + updateVariables(); + equationSystem.getIndex().addListener(this); + + // listen for state vector change to update vectorized network + equationSystem.getStateVector().addListener(this); + } + + private void updateVariables() { + Stopwatch stopwatch = Stopwatch.createStarted(); + + Arrays.fill(busVector.vRow, -1); + Arrays.fill(busVector.phRow, -1); + Arrays.fill(branchVector.a1Row, -1); + Arrays.fill(branchVector.r1Row, -1); + Arrays.fill(branchVector.v1Row, -1); + Arrays.fill(branchVector.ph1Row, -1); + Arrays.fill(branchVector.v2Row, -1); + Arrays.fill(branchVector.ph2Row, -1); + + for (Variable v : equationSystem.getIndex().getSortedVariablesToFind()) { + switch (v.getType()) { + case BUS_V: + busVector.vRow[v.getElementNum()] = v.getRow(); + break; + + case BUS_PHI: + busVector.phRow[v.getElementNum()] = v.getRow(); + break; + + case BRANCH_ALPHA1: + branchVector.a1Row[v.getElementNum()] = v.getRow(); + break; + + case BRANCH_RHO1: + branchVector.r1Row[v.getElementNum()] = v.getRow(); + break; + + default: + break; + } + } + for (int branchNum = 0; branchNum < branchVector.getSize(); branchNum++) { + if (branchVector.bus1Num[branchNum] != -1) { + branchVector.v1Row[branchNum] = busVector.vRow[branchVector.bus1Num[branchNum]]; + branchVector.ph1Row[branchNum] = busVector.phRow[branchVector.bus1Num[branchNum]]; + } + if (branchVector.bus2Num[branchNum] != -1) { + branchVector.v2Row[branchNum] = busVector.vRow[branchVector.bus2Num[branchNum]]; + branchVector.ph2Row[branchNum] = busVector.phRow[branchVector.bus2Num[branchNum]]; + } + } + + stopwatch.stop(); + LOGGER.info("AC variable vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); + } + + public void updateNetwork() { + Stopwatch stopwatch = Stopwatch.createStarted(); + + double[] state = equationSystem.getStateVector().get(); + var w = new DoubleWrapper(); + for (int branchNum = 0; branchNum < branchVector.getSize(); branchNum++) { + if (branchVector.status[branchNum] == 1) { + if (branchVector.bus1Num[branchNum] != -1 && branchVector.bus2Num[branchNum] != -1) { + double ph1 = state[branchVector.ph1Row[branchNum]]; + double ph2 = state[branchVector.ph2Row[branchNum]]; + double a1 = branchVector.a1Row[branchNum] != -1 ? state[branchVector.a1Row[branchNum]] + : branchVector.a1[branchNum]; + + double theta1 = AbstractClosedBranchAcFlowEquationTerm.theta1( + branchVector.ksi[branchNum], + ph1, + a1, + ph2); + double theta2 = AbstractClosedBranchAcFlowEquationTerm.theta2( + branchVector.ksi[branchNum], + ph1, + a1, + ph2); + double sinTheta1 = FastMath.sinAndCos(theta1, w); + double cosTheta1 = w.value; + double sinTheta2 = FastMath.sinAndCos(theta2, w); + double cosTheta2 = w.value; + + double v1 = state[branchVector.v1Row[branchNum]]; + double v2 = state[branchVector.v2Row[branchNum]]; + double r1 = branchVector.r1Row[branchNum] != -1 ? state[branchVector.r1Row[branchNum]] + : branchVector.r1[branchNum]; + + // p1 + + branchVector.p1[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.p1( + branchVector.y[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + v1, + r1, + v2, + sinTheta1); + + branchVector.dp1dv1[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.dp1dv1( + branchVector.y[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + v1, + r1, + v2, + sinTheta1); + + branchVector.dp1dv2[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.dp1dv2( + branchVector.y[branchNum], + v1, + r1, + sinTheta1); + + branchVector.dp1dph1[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.dp1dph1( + branchVector.y[branchNum], + v1, + r1, + v2, + cosTheta1); + + branchVector.dp1dph2[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.dp1dph2( + branchVector.y[branchNum], + v1, + r1, + v2, + cosTheta1); + + // q1 + + branchVector.q1[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.q1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.b1[branchNum], + v1, + r1, + v2, + cosTheta1); + + branchVector.dq1dv1[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.dq1dv1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.b1[branchNum], + v1, + r1, + v2, + cosTheta1); + + branchVector.dq1dv2[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.dq1dv2( + branchVector.y[branchNum], + v1, + r1, + cosTheta1); + + branchVector.dq1dph1[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.dq1dph1( + branchVector.y[branchNum], + v1, + r1, + v2, + sinTheta1); + + branchVector.dq1dph2[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.dq1dph2( + branchVector.y[branchNum], + v1, + r1, + v2, + sinTheta1); + + // i1 + + branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / (v1 * SQRT3 / 1000); + + // p2 + + branchVector.p2[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.p2( + branchVector.y[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g2[branchNum], + v1, + r1, + v2, + sinTheta2); + + branchVector.dp2dv1[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.dp2dv1( + branchVector.y[branchNum], + r1, + v2, + sinTheta2); + + branchVector.dp2dv2[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.dp2dv2( + branchVector.y[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g2[branchNum], + v1, + r1, + v2, + sinTheta2); + + branchVector.dp2dph1[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.dp2dph1( + branchVector.y[branchNum], + v1, + r1, + v2, + cosTheta2); + + branchVector.dp2dph2[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.dp2dph2( + branchVector.y[branchNum], + v1, + r1, + v2, + cosTheta2); + + // q2 + + branchVector.q2[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.q2( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.b2[branchNum], + v1, + r1, + v2, + cosTheta2); + + branchVector.dq2dv1[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.dq2dv1( + branchVector.y[branchNum], + r1, + v2, + cosTheta2); + + branchVector.dq2dv2[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.dq2dv2( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.b2[branchNum], + v1, + r1, + v2, + cosTheta2); + + branchVector.dq2dph1[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.dq2dph1( + branchVector.y[branchNum], + v1, + r1, + v2, + sinTheta2); + + branchVector.dq2dph2[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.dq2dph2( + branchVector.y[branchNum], + v1, + r1, + v2, + sinTheta2); + + // i2 + + branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / (v2 * SQRT3 / 1000); + } else if (branchVector.bus1Num[branchNum] != -1) { + double v1 = state[branchVector.v1Row[branchNum]]; + double r1 = branchVector.r1Row[branchNum] != -1 ? state[branchVector.r1Row[branchNum]] + : branchVector.r1[branchNum]; + + branchVector.p1[branchNum] = OpenBranchSide2ActiveFlowEquationTerm.p1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + branchVector.g2[branchNum], + branchVector.b2[branchNum], + v1, + r1); + + branchVector.q1[branchNum] = OpenBranchSide2ReactiveFlowEquationTerm.q1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.b1[branchNum], + branchVector.g2[branchNum], + branchVector.b2[branchNum], + v1, + r1); + + branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / (v1 * SQRT3 / 1000); + } else if (branchVector.bus2Num[branchNum] != -1) { + double v2 = state[branchVector.v2Row[branchNum]]; + + branchVector.p2[branchNum] = OpenBranchSide1ActiveFlowEquationTerm.p2( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + branchVector.b1[branchNum], + branchVector.g2[branchNum], + v2); + + branchVector.q2[branchNum] = OpenBranchSide1ReactiveFlowEquationTerm.q2( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + branchVector.b1[branchNum], + branchVector.b2[branchNum], + v2); + + branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / (v2 * SQRT3 / 1000); + } + } + } + + stopwatch.stop(); + LOGGER.info("AC network vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); + } + + @Override + public void onDisableChange(LfElement element, boolean disabled) { + if (element.getType() == ElementType.BRANCH) { + branchVector.status[element.getNum()] = disabled ? 0 : 1; + } + } + + @Override + public void onVariableChange(Variable variable, ChangeType changeType) { + updateVariables(); + // TODO also update state? + } + + @Override + public void onEquationChange(Equation equation, ChangeType changeType) { + // nothing to do + } + + @Override + public void onEquationTermChange(EquationTerm term) { + // nothing to do + } + + @Override + public void onStateUpdate() { + updateNetwork(); + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index 32bb0d79a7..95d77679b9 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -47,27 +47,27 @@ public static double p1(double y, double sinKsi, double g1, double v1, double r1 return r1 * v1 * (g1 * r1 * v1 + y * r1 * v1 * sinKsi - y * R2 * v2 * sinTheta); } - private static double dp1dv1(double y, double sinKsi, double g1, double v1, double r1, double v2, double sinTheta) { + public static double dp1dv1(double y, double sinKsi, double g1, double v1, double r1, double v2, double sinTheta) { return r1 * (2 * g1 * r1 * v1 + 2 * y * r1 * v1 * sinKsi - y * R2 * v2 * sinTheta); } - private static double dp1dv2(double y, double v1, double r1, double sinTheta) { + public static double dp1dv2(double y, double v1, double r1, double sinTheta) { return -y * r1 * R2 * v1 * sinTheta; } - private static double dp1dph1(double y, double v1, double r1, double v2, double cosTheta) { + public static double dp1dph1(double y, double v1, double r1, double v2, double cosTheta) { return y * r1 * R2 * v1 * v2 * cosTheta; } - private static double dp1dph2(double y, double v1, double r1, double v2, double cosTheta) { + public static double dp1dph2(double y, double v1, double r1, double v2, double cosTheta) { return -dp1dph1(y, v1, r1, v2, cosTheta); } - private static double dp1da1(double y, double v1, double r1, double v2, double cosTheta) { + public static double dp1da1(double y, double v1, double r1, double v2, double cosTheta) { return dp1dph1(y, v1, r1, v2, cosTheta); } - private static double dp1dr1(double y, double sinKsi, double g1, double v1, double r1, double v2, double sinTheta) { + public static double dp1dr1(double y, double sinKsi, double g1, double v1, double r1, double v2, double sinTheta) { return v1 * (2 * r1 * v1 * (g1 + y * sinKsi) - y * R2 * v2 * sinTheta); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index fd6bcf4505..30e259e520 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -48,28 +48,28 @@ public static double q1(double y, double cosKsi, double b1, double v1, double r1 - y * R2 * v2 * cosTheta); } - private static double dq1dv1(double y, double cosKsi, double b1, double v1, double r1, double v2, double cosTheta) { + public static double dq1dv1(double y, double cosKsi, double b1, double v1, double r1, double v2, double cosTheta) { return r1 * (-2 * b1 * r1 * v1 + 2 * y * r1 * v1 * cosKsi - y * R2 * v2 * cosTheta); } - private static double dq1dv2(double y, double v1, double r1, double cosTheta) { + public static double dq1dv2(double y, double v1, double r1, double cosTheta) { return -y * r1 * R2 * v1 * cosTheta; } - private static double dq1dph1(double y, double v1, double r1, double v2, double sinTheta) { + public static double dq1dph1(double y, double v1, double r1, double v2, double sinTheta) { return -y * r1 * R2 * v1 * v2 * sinTheta; } - private static double dq1dph2(double y, double v1, double r1, double v2, double sinTheta) { + public static double dq1dph2(double y, double v1, double r1, double v2, double sinTheta) { return -dq1dph1(y, v1, r1, v2, sinTheta); } - private static double dq1da1(double y, double v1, double r1, double v2, double sinTheta) { + public static double dq1da1(double y, double v1, double r1, double v2, double sinTheta) { return dq1dph1(y, v1, r1, v2, sinTheta); } - private static double dq1dr1(double y, double cosKsi, double b1, double v1, double r1, double v2, double cosTheta) { + public static double dq1dr1(double y, double cosKsi, double b1, double v1, double r1, double v2, double cosTheta) { return v1 * (2 * r1 * v1 * (-b1 + y * cosKsi) - y * R2 * v2 * cosTheta); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index b17aeb758d..d3c7038854 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -46,27 +46,27 @@ public static double p2(double y, double sinKsi, double g2, double v1, double r1 return R2 * v2 * (g2 * R2 * v2 - y * r1 * v1 * sinTheta + y * R2 * v2 * sinKsi); } - private static double dp2dv1(double y, double r1, double v2, double sinTheta) { + public static double dp2dv1(double y, double r1, double v2, double sinTheta) { return -y * r1 * R2 * v2 * sinTheta; } - private static double dp2dv2(double y, double sinKsi, double g2, double v1, double r1, double v2, double sinTheta) { + public static double dp2dv2(double y, double sinKsi, double g2, double v1, double r1, double v2, double sinTheta) { return R2 * (2 * g2 * R2 * v2 - y * r1 * v1 * sinTheta + 2 * y * R2 * v2 * sinKsi); } - private static double dp2dph1(double y, double v1, double r1, double v2, double cosTheta) { + public static double dp2dph1(double y, double v1, double r1, double v2, double cosTheta) { return -y * r1 * R2 * v1 * v2 * cosTheta; } - private static double dp2dph2(double y, double v1, double r1, double v2, double cosTheta) { + public static double dp2dph2(double y, double v1, double r1, double v2, double cosTheta) { return -dp2dph1(y, v1, r1, v2, cosTheta); } - private static double dp2da1(double y, double v1, double r1, double v2, double cosTheta) { + public static double dp2da1(double y, double v1, double r1, double v2, double cosTheta) { return dp2dph1(y, v1, r1, v2, cosTheta); } - private static double dp2dr1(double y, double v1, double v2, double sinTheta) { + public static double dp2dr1(double y, double v1, double v2, double sinTheta) { return -y * R2 * v1 * v2 * sinTheta; } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index f010bed129..6819f81d30 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -46,27 +46,27 @@ public static double q2(double y, double cosKsi, double b2, double v1, double r1 return R2 * v2 * (-b2 * R2 * v2 - y * r1 * v1 * cosTheta + y * R2 * v2 * cosKsi); } - private static double dq2dv1(double y, double r1, double v2, double cosTheta) { + public static double dq2dv1(double y, double r1, double v2, double cosTheta) { return -y * r1 * R2 * v2 * cosTheta; } - private static double dq2dv2(double y, double cosKsi, double b2, double v1, double r1, double v2, double cosTheta) { + public static double dq2dv2(double y, double cosKsi, double b2, double v1, double r1, double v2, double cosTheta) { return R2 * (-2 * b2 * R2 * v2 - y * r1 * v1 * cosTheta + 2 * y * R2 * v2 * cosKsi); } - private static double dq2dph1(double y, double v1, double r1, double v2, double sinTheta) { + public static double dq2dph1(double y, double v1, double r1, double v2, double sinTheta) { return y * r1 * R2 * v1 * v2 * sinTheta; } - private static double dq2dph2(double y, double v1, double r1, double v2, double sinTheta) { + public static double dq2dph2(double y, double v1, double r1, double v2, double sinTheta) { return -dq2dph1(y, v1, r1, v2, sinTheta); } - private static double dq2da1(double y, double v1, double r1, double v2, double sinTheta) { + public static double dq2da1(double y, double v1, double r1, double v2, double sinTheta) { return dq2dph1(y, v1, r1, v2, sinTheta); } - private static double dq2dr1(double y, double v1, double v2, double cosTheta) { + public static double dq2dr1(double y, double v1, double v2, double cosTheta) { return -y * R2 * v1 * v2 * cosTheta; } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java index 1b411b3054..0b80a6e1ea 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java @@ -33,12 +33,12 @@ private double v2() { return sv.get(v2Var.getRow()); } - private static double p2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { + public static double p2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { double shunt = shunt(y, cosKsi, sinKsi, g1, b1); return R2 * R2 * v2 * v2 * (g2 + y * y * g1 / shunt + (b1 * b1 + g1 * g1) * y * sinKsi / shunt); } - private static double dp2dv2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { + public static double dp2dv2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { double shunt = shunt(y, cosKsi, sinKsi, g1, b1); return 2 * R2 * R2 * v2 * (g2 + y * y * g1 / shunt + (b1 * b1 + g1 * g1) * y * sinKsi / shunt); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java index bf9da8928a..f6bc3a9789 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java @@ -33,12 +33,12 @@ private double v2() { return sv.get(v2Var.getRow()); } - private static double q2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { + public static double q2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { double shunt = shunt(y, cosKsi, sinKsi, g1, b1); return -R2 * R2 * v2 * v2 * (b2 + y * y * b1 / shunt - (b1 * b1 + g1 * g1) * y * cosKsi / shunt); } - private static double dq2dv2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { + public static double dq2dv2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { double shunt = shunt(y, cosKsi, sinKsi, g1, b1); return -2 * v2 * R2 * R2 * (b2 + y * y * b1 / shunt - (b1 * b1 + g1 * g1) * y * cosKsi / shunt); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java index 29da3b4d9a..769f278d92 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java @@ -35,12 +35,12 @@ private double r1() { return element.getPiModel().getR1(); } - private static double p1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { + public static double p1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { double shunt = shunt(y, cosKsi, sinKsi, g2, b2); return r1 * r1 * v1 * v1 * (g1 + y * y * g2 / shunt + (b2 * b2 + g2 * g2) * y * sinKsi / shunt); } - private static double dp1dv1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { + public static double dp1dv1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { double shunt = shunt(y, cosKsi, sinKsi, g2, b2); return 2 * r1 * r1 * v1 * (g1 + y * y * g2 / shunt + (b2 * b2 + g2 * g2) * y * sinKsi / shunt); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java index 77a3c5e770..702b7d9407 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java @@ -35,12 +35,12 @@ private double r1() { return element.getPiModel().getR1(); } - private static double q1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { + public static double q1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { double shunt = shunt(y, cosKsi, sinKsi, g2, b2); return -r1 * r1 * v1 * v1 * (b1 + y * y * b2 / shunt - (b2 * b2 + g2 * g2) * y * cosKsi / shunt); } - private static double dq1dv1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { + public static double dq1dv1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { double shunt = shunt(y, cosKsi, sinKsi, g2, b2); return -2 * v1 * r1 * r1 * (b1 + y * y * b2 / shunt - (b2 * b2 + g2 * g2) * y * cosKsi / shunt); } From abc78d18e1be8ab771118d37e1d5ed6771f81b93 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 15:11:27 +0200 Subject: [PATCH 02/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AbstractBranchAcFlowEquationTerm.java | 7 +- ...bstractClosedBranchAcFlowEquationTerm.java | 6 +- ...ractOpenSide1BranchAcFlowEquationTerm.java | 6 +- ...ractOpenSide2BranchAcFlowEquationTerm.java | 6 +- .../ac/equations/AcEquationSystemCreator.java | 116 +++++++++--------- .../ac/equations/AcEquationSystemUpdater.java | 7 +- .../ac/equations/AcNetworkVector.java | 4 + ...osedBranchSide1ActiveFlowEquationTerm.java | 6 +- ...anchSide1CurrentMagnitudeEquationTerm.java | 6 +- ...edBranchSide1ReactiveFlowEquationTerm.java | 6 +- ...osedBranchSide2ActiveFlowEquationTerm.java | 6 +- ...anchSide2CurrentMagnitudeEquationTerm.java | 6 +- ...edBranchSide2ReactiveFlowEquationTerm.java | 6 +- ...OpenBranchSide1ActiveFlowEquationTerm.java | 6 +- ...anchSide1CurrentMagnitudeEquationTerm.java | 6 +- ...enBranchSide1ReactiveFlowEquationTerm.java | 6 +- ...OpenBranchSide2ActiveFlowEquationTerm.java | 6 +- ...anchSide2CurrentMagnitudeEquationTerm.java | 6 +- ...enBranchSide2ReactiveFlowEquationTerm.java | 6 +- .../powsybl/openloadflow/EquationsTest.java | 41 ++++--- .../network/impl/LfSwitchTest.java | 11 +- 21 files changed, 148 insertions(+), 128 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java index 7ec1e1d418..062318e482 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java @@ -10,11 +10,15 @@ import com.powsybl.openloadflow.network.LfBranch; import com.powsybl.openloadflow.network.PiModel; +import java.util.Objects; + /** * @author Geoffroy Jamgotchian */ abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationTerm { + protected AcBranchVector branchVector; + protected final double b1; protected final double b2; protected final double g1; @@ -22,8 +26,9 @@ abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationT protected final double y; protected final double ksi; - protected AbstractBranchAcFlowEquationTerm(LfBranch branch) { + protected AbstractBranchAcFlowEquationTerm(LfBranch branch, AcBranchVector branchVector) { super(branch); + this.branchVector = Objects.requireNonNull(branchVector); PiModel piModel = branch.getPiModel(); if (piModel.getR() == 0 && piModel.getX() == 0) { throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java index 24b900d866..d43f7a11c4 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java @@ -37,9 +37,9 @@ public abstract class AbstractClosedBranchAcFlowEquationTerm extends AbstractBra protected final List> variables = new ArrayList<>(); - protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch); + protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, branchVector); Objects.requireNonNull(bus1); Objects.requireNonNull(bus2); Objects.requireNonNull(variableSet); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java index f58583a17b..9b7196c212 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java @@ -21,9 +21,9 @@ abstract class AbstractOpenSide1BranchAcFlowEquationTerm extends AbstractBranchA protected final List> variables; protected AbstractOpenSide1BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType, - LfBus bus, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch); + LfBus bus, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, branchVector); variables = List.of(variableSet.getVariable(bus.getNum(), variableType)); if (deriveA1 || deriveR1) { throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branch.getId()); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java index 1faf8fa1d6..1ca10a9a15 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java @@ -21,9 +21,9 @@ abstract class AbstractOpenSide2BranchAcFlowEquationTerm extends AbstractBranchA protected final List> variables; protected AbstractOpenSide2BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType, - LfBus bus, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch); + LfBus bus, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, branchVector); variables = List.of(variableSet.getVariable(bus.getNum(), variableType)); if (deriveA1 || deriveR1) { throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branch.getId()); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 1753fcdb98..d3acc8631a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -60,9 +60,9 @@ private void createBusEquation(LfBus bus, createShuntEquations(bus, equationSystem); } - private void createVoltageControlEquations(EquationSystem equationSystem) { + private void createVoltageControlEquations(AcBranchVector branchVector, EquationSystem equationSystem) { for (LfBus bus : network.getBuses()) { - createGeneratorVoltageControlEquations(bus, equationSystem); + createGeneratorVoltageControlEquations(bus, branchVector, equationSystem); createTransformerVoltageControlEquations(bus, equationSystem); createShuntVoltageControlEquations(bus, equationSystem); } @@ -74,23 +74,23 @@ private void createBusesEquations(EquationSystem } } - private void createGeneratorVoltageControlEquations(LfBus bus, + private void createGeneratorVoltageControlEquations(LfBus bus, AcBranchVector branchVector, EquationSystem equationSystem) { bus.getGeneratorVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> { if (bus.isGeneratorVoltageControlled()) { if (voltageControl.isLocalControl()) { - createGeneratorLocalVoltageControlEquation(bus, equationSystem); + createGeneratorLocalVoltageControlEquation(bus, branchVector, equationSystem); } else { - createGeneratorRemoteVoltageControlEquations(voltageControl, equationSystem); + createGeneratorRemoteVoltageControlEquations(branchVector, voltageControl, equationSystem); } updateGeneratorVoltageControl(voltageControl, equationSystem); } }); } - private void createGeneratorLocalVoltageControlEquation(LfBus bus, + private void createGeneratorLocalVoltageControlEquation(LfBus bus, AcBranchVector branchVector, EquationSystem equationSystem) { if (bus.hasGeneratorsWithSlope()) { // take first generator with slope: network loading ensures that there's only one generator with slope @@ -100,7 +100,7 @@ private void createGeneratorLocalVoltageControlEquation(LfBus bus, // equation is: V + slope * qSVC = targetV // which is modeled here with: V + slope * (sum_branch qBranch) = TargetV - slope * qLoads + slope * qGenerators equationSystem.getEquation(bus.getNum(), AcEquationType.BUS_TARGET_V).orElseThrow() - .addTerms(createReactiveTerms(bus, equationSystem.getVariableSet(), creationParameters) + .addTerms(createReactiveTerms(bus, branchVector, equationSystem.getVariableSet(), creationParameters) .stream() .map(term -> term.multiply(slope)) .collect(Collectors.toList())); @@ -109,13 +109,13 @@ private void createGeneratorLocalVoltageControlEquation(LfBus bus, equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_Q); } - private static void createReactivePowerControlBranchEquation(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, - boolean deriveA1, boolean deriveR1) { + private static void createReactivePowerControlBranchEquation(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + EquationSystem equationSystem, boolean deriveA1, boolean deriveR1) { if (bus1 != null && bus2 != null) { branch.getReactivePowerControl().ifPresent(rpc -> { EquationTerm q = rpc.getControlledSide() == ControlledSide.ONE - ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1) + : new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_Q) .addTerm(q); @@ -151,7 +151,7 @@ private static void createShuntEquations(LfBus bus, EquationSystem createShuntEquation(shunt, bus, equationSystem, false)); } - private static void createReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, EquationSystem equationSystem, + private static void createReactivePowerDistributionEquations(AcBranchVector branchVector, GeneratorVoltageControl voltageControl, EquationSystem equationSystem, AcEquationSystemCreationParameters creationParameters) { List controllerBuses = voltageControl.getMergedControllerElements(); for (LfBus controllerBus : controllerBuses) { @@ -161,12 +161,12 @@ private static void createReactivePowerDistributionEquations(GeneratorVoltageCon // which can be rewritten in a more simple way // 0 = (qPercent_i - 1) * q_i + qPercent_i * sum_j(q_j) where j are all the voltage controller buses except i Equation zero = equationSystem.createEquation(controllerBus, AcEquationType.DISTR_Q) - .addTerms(createReactiveTerms(controllerBus, equationSystem.getVariableSet(), creationParameters).stream() + .addTerms(createReactiveTerms(controllerBus, branchVector, equationSystem.getVariableSet(), creationParameters).stream() .map(term -> term.multiply(() -> controllerBus.getRemoteVoltageControlReactivePercent() - 1)) .collect(Collectors.toList())); for (LfBus otherControllerBus : controllerBuses) { if (otherControllerBus != controllerBus) { - zero.addTerms(createReactiveTerms(otherControllerBus, equationSystem.getVariableSet(), creationParameters).stream() + zero.addTerms(createReactiveTerms(otherControllerBus, branchVector, equationSystem.getVariableSet(), creationParameters).stream() .map(term -> term.multiply(controllerBus::getRemoteVoltageControlReactivePercent)) .collect(Collectors.toList())); } @@ -174,26 +174,26 @@ private static void createReactivePowerDistributionEquations(GeneratorVoltageCon } } - public static void recreateReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, + public static void recreateReactivePowerDistributionEquations(AcBranchVector branchVector, GeneratorVoltageControl voltageControl, EquationSystem equationSystem, AcEquationSystemCreationParameters parameters) { for (LfBus controllerBus : voltageControl.getMergedControllerElements()) { equationSystem.removeEquation(controllerBus.getNum(), AcEquationType.DISTR_Q); } if (!voltageControl.isLocalControl()) { - createReactivePowerDistributionEquations(voltageControl, equationSystem, parameters); + createReactivePowerDistributionEquations(branchVector, voltageControl, equationSystem, parameters); } updateGeneratorVoltageControl(voltageControl, equationSystem); } - private void createGeneratorRemoteVoltageControlEquations(GeneratorVoltageControl voltageControl, + private void createGeneratorRemoteVoltageControlEquations(AcBranchVector branchVector, GeneratorVoltageControl voltageControl, EquationSystem equationSystem) { for (LfBus controllerBus : voltageControl.getMergedControllerElements()) { equationSystem.createEquation(controllerBus, AcEquationType.BUS_TARGET_Q); } // create reactive power distribution equations at voltage controller buses - createReactivePowerDistributionEquations(voltageControl, equationSystem, creationParameters); + createReactivePowerDistributionEquations(branchVector, voltageControl, equationSystem, creationParameters); } static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, @@ -282,7 +282,7 @@ static void updateRemoteGeneratorVoltageControlEquations(GeneratorVoltageControl updateRemoteVoltageControlEquations(voltageControl, equationSystem, AcEquationType.DISTR_Q, AcEquationType.BUS_TARGET_Q); } - private static List> createReactiveTerms(LfBus controllerBus, + private static List> createReactiveTerms(LfBus controllerBus, AcBranchVector branchVector, VariableSet variableSet, AcEquationSystemCreationParameters creationParameters) { List> terms = new ArrayList<>(); @@ -303,12 +303,12 @@ private static List> createReactive boolean deriveR1 = isDeriveR1(branch); if (branch.getBus1() == controllerBus) { LfBus otherSideBus = branch.getBus2(); - q = otherSideBus != null ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, otherSideBus, variableSet, deriveA1, deriveR1) - : new OpenBranchSide2ReactiveFlowEquationTerm(branch, controllerBus, variableSet, deriveA1, deriveR1); + q = otherSideBus != null ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, otherSideBus, branchVector, variableSet, deriveA1, deriveR1) + : new OpenBranchSide2ReactiveFlowEquationTerm(branch, controllerBus, branchVector, variableSet, deriveA1, deriveR1); } else { LfBus otherSideBus = branch.getBus1(); - q = otherSideBus != null ? new ClosedBranchSide2ReactiveFlowEquationTerm(branch, otherSideBus, controllerBus, variableSet, deriveA1, deriveR1) - : new OpenBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, variableSet, deriveA1, deriveR1); + q = otherSideBus != null ? new ClosedBranchSide2ReactiveFlowEquationTerm(branch, otherSideBus, controllerBus, branchVector, variableSet, deriveA1, deriveR1) + : new OpenBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, branchVector, variableSet, deriveA1, deriveR1); } } terms.add(q); @@ -433,8 +433,8 @@ private static void createNonImpedantBranch(LfBranch branch, LfBus bus1, LfBus b } } - private static void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, - boolean deriveA1, boolean deriveR1) { + private static void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + EquationSystem equationSystem, boolean deriveA1, boolean deriveR1) { if (deriveA1) { EquationTerm a1 = equationSystem.getVariable(branch.getNum(), AcVariableType.BRANCH_ALPHA1) .createTerm(); @@ -451,8 +451,8 @@ private static void createTransformerPhaseControlEquations(LfBranch branch, LfBu } EquationTerm p = phaseControl.getControlledSide() == ControlledSide.ONE - ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1) + : new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_P) .addTerm(p) .setActive(false); // by default BRANCH_TARGET_ALPHA1 is active and BRANCH_TARGET_P inactive @@ -600,7 +600,7 @@ private static boolean isDeriveR1(LfBranch branch) { return branch.isVoltageController(); } - private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, + private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, EquationSystem equationSystem) { EquationTerm p1 = null; EquationTerm q1 = null; @@ -611,20 +611,20 @@ private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1 = isDeriveA1(branch, creationParameters); boolean deriveR1 = isDeriveR1(branch); if (bus1 != null && bus2 != null) { - p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); } else if (bus1 != null) { - p1 = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, equationSystem.getVariableSet(), deriveA1, deriveR1); - q1 = new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, equationSystem.getVariableSet(), deriveA1, deriveR1); - i1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + q1 = new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + i1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); } else if (bus2 != null) { - p2 = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - q2 = new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + p2 = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + q2 = new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + i2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); } if (p1 != null) { @@ -662,9 +662,9 @@ private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, branch.setI2(i2); } - createReactivePowerControlBranchEquation(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createReactivePowerControlBranchEquation(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); - createTransformerPhaseControlEquations(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createTransformerPhaseControlEquations(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); } private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem equationSystem) { @@ -691,23 +691,23 @@ private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem equationSystem) { // create zero and non zero impedance branch equations if (branch.isZeroImpedance(LoadFlowModel.AC)) { createNonImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem, branch.isSpanningTreeEdge(LoadFlowModel.AC)); } else { - createImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem); + createImpedantBranch(branch, branch.getBus1(), branch.getBus2(), branchVector, equationSystem); } } - private void createBranchesEquations(EquationSystem equationSystem) { + private void createBranchesEquations(AcBranchVector branchVector, EquationSystem equationSystem) { for (LfBranch branch : network.getBranches()) { - createBranchEquations(branch, equationSystem); + createBranchEquations(branch, branchVector, equationSystem); } } - private List> createActiveInjectionTerms(LfBus bus, + private List> createActiveInjectionTerms(LfBus bus, AcBranchVector branchVector, VariableSet variableSet) { List> terms = new ArrayList<>(); for (LfBranch branch : bus.getBranches()) { @@ -724,13 +724,13 @@ private List> createActiveInjection boolean deriveR1 = isDeriveR1(branch); if (branch.getBus1() == bus) { LfBus otherSideBus = branch.getBus2(); - var p = otherSideBus != null ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus, otherSideBus, variableSet, deriveA1, deriveR1) - : new OpenBranchSide2ActiveFlowEquationTerm(branch, bus, variableSet, deriveA1, deriveR1); + var p = otherSideBus != null ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus, otherSideBus, branchVector, variableSet, deriveA1, deriveR1) + : new OpenBranchSide2ActiveFlowEquationTerm(branch, bus, branchVector, variableSet, deriveA1, deriveR1); terms.add(p); } else { LfBus otherSideBus = branch.getBus1(); - var p = otherSideBus != null ? new ClosedBranchSide2ActiveFlowEquationTerm(branch, otherSideBus, bus, variableSet, deriveA1, deriveR1) - : new OpenBranchSide1ActiveFlowEquationTerm(branch, bus, variableSet, deriveA1, deriveR1); + var p = otherSideBus != null ? new ClosedBranchSide2ActiveFlowEquationTerm(branch, otherSideBus, bus, branchVector, variableSet, deriveA1, deriveR1) + : new OpenBranchSide1ActiveFlowEquationTerm(branch, bus, branchVector, variableSet, deriveA1, deriveR1); terms.add(p); } } @@ -738,7 +738,7 @@ private List> createActiveInjection return terms; } - private void createMultipleSlackBusesEquations(EquationSystem equationSystem) { + private void createMultipleSlackBusesEquations(AcBranchVector branchVector, EquationSystem equationSystem) { List slackBuses = network.getSlackBuses(); if (slackBuses.size() > 1) { LfBus firstSlackBus = slackBuses.get(0); @@ -748,8 +748,8 @@ private void createMultipleSlackBusesEquations(EquationSystem create() { AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem); createBusesEquations(equationSystem); - createMultipleSlackBusesEquations(equationSystem); - createBranchesEquations(equationSystem); + createMultipleSlackBusesEquations(networkVector.getBranchVector(), equationSystem); + createBranchesEquations(networkVector.getBranchVector(), equationSystem); for (LfHvdc hvdc : network.getHvdcs()) { createHvdcAcEmulationEquations(hvdc, equationSystem); } - createVoltageControlEquations(equationSystem); + createVoltageControlEquations(networkVector.getBranchVector(), equationSystem); EquationSystemPostProcessor.findAll().forEach(pp -> pp.onCreate(equationSystem)); - network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(equationSystem, creationParameters))); + network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(networkVector, equationSystem, creationParameters))); networkVector.startListening(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java index 511a8168f1..056f36da8a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java @@ -18,11 +18,14 @@ */ public class AcEquationSystemUpdater extends AbstractEquationSystemUpdater { + private final AcNetworkVector networkVector; + private final AcEquationSystemCreationParameters parameters; - public AcEquationSystemUpdater(EquationSystem equationSystem, + public AcEquationSystemUpdater(AcNetworkVector networkVector, EquationSystem equationSystem, AcEquationSystemCreationParameters parameters) { super(equationSystem, LoadFlowModel.AC); + this.networkVector = Objects.requireNonNull(networkVector); this.parameters = Objects.requireNonNull(parameters); } @@ -112,7 +115,7 @@ private void recreateDistributionEquations(LfZeroImpedanceNetwork network) { for (LfBus bus : network.getGraph().vertexSet()) { bus.getGeneratorVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateReactivePowerDistributionEquations(voltageControl, equationSystem, parameters)); + .ifPresent(voltageControl -> AcEquationSystemCreator.recreateReactivePowerDistributionEquations(networkVector.getBranchVector(), voltageControl, equationSystem, parameters)); bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, equationSystem)); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index e34e420c5c..766a218337 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -40,6 +40,10 @@ public AcNetworkVector(LfNetwork network, EquationSystem variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1); + public ClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index 345285d82f..b8ed7ebfe8 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -23,9 +23,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1CurrentMagnitudeEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1); + public ClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index 30e259e520..aa9ab8e4a3 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -22,9 +22,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1ReactiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1); + public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index d3c7038854..61f956191b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -22,9 +22,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2ActiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1); + public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java index 7e50e77b8f..7bc89699ec 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java @@ -23,9 +23,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2CurrentMagnitudeEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1); + public ClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index 6819f81d30..17d0ef4c9e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -22,9 +22,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2ReactiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1); + public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java index 0b80a6e1ea..be33223235 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java @@ -23,9 +23,9 @@ public class OpenBranchSide1ActiveFlowEquationTerm extends AbstractOpenSide1Bran private final Variable v2Var; - public OpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus2, variableSet, deriveA1, deriveR1); + public OpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus2, branchVector, variableSet, deriveA1, deriveR1); v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java index f08d47c2de..897c96af45 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java @@ -26,9 +26,9 @@ public class OpenBranchSide1CurrentMagnitudeEquationTerm extends AbstractOpenSid private final Variable ph2Var; - public OpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus2, variableSet, deriveA1, deriveR1); + public OpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus2, branchVector, variableSet, deriveA1, deriveR1); v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); ph2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_PHI); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java index f6bc3a9789..a2c71f0605 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java @@ -23,9 +23,9 @@ public class OpenBranchSide1ReactiveFlowEquationTerm extends AbstractOpenSide1Br private final Variable v2Var; - public OpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus2, variableSet, deriveA1, deriveR1); + public OpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus2, branchVector, variableSet, deriveA1, deriveR1); v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java index 769f278d92..158c444cce 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java @@ -21,9 +21,9 @@ public class OpenBranchSide2ActiveFlowEquationTerm extends AbstractOpenSide2Bran private final Variable v1Var; - public OpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus1, variableSet, deriveA1, deriveR1); + public OpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus1, branchVector, variableSet, deriveA1, deriveR1); v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java index 0e53368045..f67d49ebe5 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java @@ -26,9 +26,9 @@ public class OpenBranchSide2CurrentMagnitudeEquationTerm extends AbstractOpenSid private Variable r1Var; - public OpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus1, variableSet, deriveA1, deriveR1); + public OpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus1, branchVector, variableSet, deriveA1, deriveR1); v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); ph1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_PHI); if (deriveR1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java index 702b7d9407..570e99693a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java @@ -21,9 +21,9 @@ public class OpenBranchSide2ReactiveFlowEquationTerm extends AbstractOpenSide2Br private final Variable v1Var; - public OpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, VariableSet variableSet, - boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus1, variableSet, deriveA1, deriveR1); + public OpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcBranchVector branchVector, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus1, branchVector, variableSet, deriveA1, deriveR1); v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); } diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 5511d49e88..5491fbcddb 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -145,53 +145,58 @@ void branchTest() { var sv = new StateVector(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); + LfNetwork lfNetwork = Mockito.mock(LfNetwork.class); + EquationSystem equationSystem = new EquationSystem<>(); + AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem); + AcBranchVector branchVector = networkVector.getBranchVector(); + // closed branch equations assertArrayEquals(new double[] {41.78173051479356, 48.66261692116701, 138.21343172859858, 29.31710523088579, -138.21343172859858, 54.62161149356045, 138.21343172859858, Double.NaN, 270.81476537421185}, - eval(new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-3.500079625302254, 122.46444997806617, 31.42440177840898, -128.9449438332101, -31.42440177840898, 137.46086897280827, 31.42440177840898, Double.NaN, 162.40477689607334}, - eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {39.13246485286217, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, - eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-40.6365773800554, -48.52391742324069, -131.8614376204652, -27.319027760225953, 131.8614376204652, -54.4659275092331, -131.8614376204652, Double.NaN, -262.1703103131649}, - eval(new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {16.04980301110306, -123.06939783256767, 51.99045110393844, 152.96594042215764, -51.99045110393844, -138.1398958886022, 51.99045110393844, Double.NaN, -56.2529021950738}, - eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {40.7613721648136, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, - eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); // open branch equations assertArrayEquals(new double[] {0.1717595025847833, Double.NaN, Double.NaN, 0.3204828812456483, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.37520249405559764, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.15652310047954035, Double.NaN, Double.NaN, 0.2920535601715773, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2ActiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide2ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.331495628053771, Double.NaN, Double.NaN, -0.6185315653587614, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.3420075216110214, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); // assert current equation is consistent with active and reactive power ones - var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); + var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); p1Eq.setStateVector(sv); double p1 = p1Eq.eval(); - var q1Eq = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); + var q1Eq = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); q1Eq.setStateVector(sv); double q1 = q1Eq.eval(); - var i1Eq = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true); + var i1Eq = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); i1Eq.setStateVector(sv); double i1 = i1Eq.eval(); assertEquals(i1, Math.hypot(p1, q1) / V_1, 10e-14); - var p2Eq = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); + var p2Eq = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); p2Eq.setStateVector(sv); double p2 = p2Eq.eval(); - var q2Eq = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); + var q2Eq = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); q2Eq.setStateVector(sv); double q2 = q2Eq.eval(); - var i2Eq = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true); + var i2Eq = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); i2Eq.setStateVector(sv); double i2 = i2Eq.eval(); assertEquals(i2, Math.hypot(p2, q2) / V_2, 10e-14); diff --git a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java index 080b9ba688..8e3d5b8337 100644 --- a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java +++ b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java @@ -14,6 +14,7 @@ import com.powsybl.openloadflow.OpenLoadFlowParameters; import com.powsybl.openloadflow.ac.equations.*; import com.powsybl.openloadflow.ac.AcLoadFlowParameters; +import com.powsybl.openloadflow.equations.EquationSystem; import com.powsybl.openloadflow.equations.EquationTerm; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.graph.EvenShiloachGraphDecrementalConnectivityFactory; @@ -69,16 +70,18 @@ void getterTest() { void setterTest() { lfSwitch.getPiModel().setX(LfNetworkParameters.LOW_IMPEDANCE_THRESHOLD_DEFAULT_VALUE); + EquationSystem equationSystem = new EquationSystem<>(); + AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem); VariableSet variableSet = new VariableSet<>(); - EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); - EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); + EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); + EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); lfSwitch.setP1(p1); assertEquals(Double.NaN, lfSwitch.getP1().eval()); lfSwitch.setP2(p2); assertEquals(Double.NaN, lfSwitch.getP2().eval()); - EquationTerm i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); - EquationTerm i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); + EquationTerm i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); + EquationTerm i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); lfSwitch.setI1(i1); assertEquals(Double.NaN, lfSwitch.getP1().eval()); lfSwitch.setI2(i2); From c915bae0c4f0c564dbbcfd2d8fb1a204792ca2aa Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 19:54:51 +0200 Subject: [PATCH 03/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AbstractBranchAcFlowEquationTerm.java | 4 +- .../ac/equations/AcBranchVector.java | 22 +++++--- .../ac/equations/AcBusVector.java | 4 +- .../ac/equations/AcEquationSystemCreator.java | 6 +-- .../ac/equations/AcNetworkVector.java | 54 +++++++++++++++---- ...osedBranchSide1ActiveFlowEquationTerm.java | 15 +++--- .../powsybl/openloadflow/EquationsTest.java | 28 ++++++++-- .../network/impl/LfSwitchTest.java | 3 +- 8 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java index 062318e482..e042191609 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java @@ -17,7 +17,8 @@ */ abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationTerm { - protected AcBranchVector branchVector; + protected final AcBranchVector branchVector; + protected final int num; protected final double b1; protected final double b2; @@ -29,6 +30,7 @@ abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationT protected AbstractBranchAcFlowEquationTerm(LfBranch branch, AcBranchVector branchVector) { super(branch); this.branchVector = Objects.requireNonNull(branchVector); + this.num = branch.getNum(); PiModel piModel = branch.getPiModel(); if (piModel.getR() == 0 && piModel.getX() == 0) { throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java index 797b3cf44b..5e52876689 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java @@ -34,13 +34,16 @@ public class AcBranchVector { final int[] status; - final int[] v1Row; - final int[] v2Row; - final int[] ph1Row; - final int[] ph2Row; + final boolean[] deriveA1; + final boolean[] deriveR1; - final int[] a1Row; - final int[] r1Row; + public final int[] v1Row; + public final int[] v2Row; + public final int[] ph1Row; + public final int[] ph2Row; + + public final int[] a1Row; + public final int[] r1Row; final double[] p1; final double[] p2; @@ -77,7 +80,7 @@ public class AcBranchVector { final double[] dq2da1; final double[] dq2dr1; - public AcBranchVector(List branches) { + public AcBranchVector(List branches, AcEquationSystemCreationParameters creationParameters) { int size = branches.size(); bus1Num = new int[size]; bus2Num = new int[size]; @@ -93,6 +96,9 @@ public AcBranchVector(List branches) { a1 = new double[size]; status = new int[size]; + deriveA1 = new boolean[size]; + deriveR1 = new boolean[size]; + v1Row = new int[size]; v2Row = new int[size]; ph1Row = new int[size]; @@ -156,6 +162,8 @@ public AcBranchVector(List branches) { r1[i] = piModel.getR1(); a1[i] = piModel.getA1(); status[i] = branch.isDisabled() ? 0 : 1; + deriveA1[i] = AcEquationSystemCreator.isDeriveA1(branch, creationParameters); + deriveR1[i] = AcEquationSystemCreator.isDeriveR1(branch); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java index 7c13362950..03772c739c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java @@ -15,8 +15,8 @@ */ public class AcBusVector { - final int[] vRow; - final int[] phRow; + public final int[] vRow; + public final int[] phRow; public AcBusVector(List buses) { vRow = new int[buses.size()]; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index d3acc8631a..955ee3a43a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -591,12 +591,12 @@ public static void recreateShuntSusceptanceDistributionEquations(ShuntVoltageCon updateShuntVoltageControlEquations(voltageControl, equationSystem); } - private static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParameters creationParameters) { + static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParameters creationParameters) { return branch.isPhaseController() || (creationParameters.isForceA1Var() && branch.hasPhaseControllerCapability() && branch.isConnectedAtBothSides()); } - private static boolean isDeriveR1(LfBranch branch) { + static boolean isDeriveR1(LfBranch branch) { return branch.isVoltageController(); } @@ -760,7 +760,7 @@ public EquationSystem create() { EquationSystem equationSystem = new EquationSystem<>(); - AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); createBusesEquations(equationSystem); createMultipleSlackBusesEquations(networkVector.getBranchVector(), equationSystem); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 766a218337..820f5bb3d6 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -33,11 +33,16 @@ public class AcNetworkVector extends AbstractLfNetworkListener private final AcBusVector busVector; private final AcBranchVector branchVector; - public AcNetworkVector(LfNetwork network, EquationSystem equationSystem) { + public AcNetworkVector(LfNetwork network, EquationSystem equationSystem, + AcEquationSystemCreationParameters creationParameters) { this.network = Objects.requireNonNull(network); this.equationSystem = Objects.requireNonNull(equationSystem); busVector = new AcBusVector(network.getBuses()); - branchVector = new AcBranchVector(network.getBranches()); + branchVector = new AcBranchVector(network.getBranches(), creationParameters); + } + + public AcBusVector getBusVector() { + return busVector; } public AcBranchVector getBranchVector() { @@ -56,7 +61,7 @@ public void startListening() { equationSystem.getStateVector().addListener(this); } - private void updateVariables() { + public void updateVariables() { Stopwatch stopwatch = Stopwatch.createStarted(); Arrays.fill(busVector.vRow, -1); @@ -69,27 +74,36 @@ private void updateVariables() { Arrays.fill(branchVector.ph2Row, -1); for (Variable v : equationSystem.getIndex().getSortedVariablesToFind()) { + int num = v.getElementNum(); + int row = v.getRow(); switch (v.getType()) { case BUS_V: - busVector.vRow[v.getElementNum()] = v.getRow(); + busVector.vRow[num] = row; break; case BUS_PHI: - busVector.phRow[v.getElementNum()] = v.getRow(); + busVector.phRow[num] = row; break; case BRANCH_ALPHA1: - branchVector.a1Row[v.getElementNum()] = v.getRow(); + branchVector.a1Row[num] = branchVector.deriveA1[num] ? row : -1; break; case BRANCH_RHO1: - branchVector.r1Row[v.getElementNum()] = v.getRow(); + branchVector.r1Row[num] = branchVector.deriveR1[num] ? row : -1; break; default: break; } } + updateBranchVariables(); + + stopwatch.stop(); + LOGGER.info("AC variable vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); + } + + public void updateBranchVariables() { for (int branchNum = 0; branchNum < branchVector.getSize(); branchNum++) { if (branchVector.bus1Num[branchNum] != -1) { branchVector.v1Row[branchNum] = busVector.vRow[branchVector.bus1Num[branchNum]]; @@ -100,9 +114,6 @@ private void updateVariables() { branchVector.ph2Row[branchNum] = busVector.phRow[branchVector.bus2Num[branchNum]]; } } - - stopwatch.stop(); - LOGGER.info("AC variable vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); } public void updateNetwork() { @@ -178,6 +189,22 @@ public void updateNetwork() { v2, cosTheta1); + branchVector.dp1da1[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.dp1da1( + branchVector.y[branchNum], + v1, + r1, + v2, + cosTheta1); + + branchVector.dp1dr1[branchNum] = ClosedBranchSide1ActiveFlowEquationTerm.dp1dr1( + branchVector.y[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + v1, + r1, + v2, + sinTheta1); + // q1 branchVector.q1[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.q1( @@ -368,6 +395,13 @@ public void onDisableChange(LfElement element, boolean disabled) { } } + @Override + public void onTapPositionChange(LfBranch branch, int oldPosition, int newPosition) { + PiModel piModel = branch.getPiModel(); + branchVector.a1[branch.getNum()] = piModel.getA1(); + branchVector.r1[branch.getNum()] = piModel.getR1(); + } + @Override public void onVariableChange(Variable variable, ChangeType changeType) { updateVariables(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index bbcaa020e6..7aec4f4d25 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -73,25 +73,24 @@ public static double dp1dr1(double y, double sinKsi, double g1, double v1, doubl @Override public double eval() { - return p1(y, FastMath.sin(ksi), g1, v1(), r1(), v2(), FastMath.sin(theta1(ksi, ph1(), a1(), ph2()))); + return branchVector.p1[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double theta = theta1(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return dp1dv1(y, FastMath.sin(ksi), g1, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dp1dv1[num]; } else if (variable.equals(v2Var)) { - return dp1dv2(y, v1(), r1(), FastMath.sin(theta)); + return branchVector.dp1dv2[num]; } else if (variable.equals(ph1Var)) { - return dp1dph1(y, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dp1dph1[num]; } else if (variable.equals(ph2Var)) { - return dp1dph2(y, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dp1dph2[num]; } else if (variable.equals(a1Var)) { - return dp1da1(y, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dp1da1[num]; } else if (variable.equals(r1Var)) { - return dp1dr1(y, FastMath.sin(ksi), g1, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dp1dr1[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 5491fbcddb..631c6830bf 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -94,6 +94,8 @@ private static & Quantity, E extends Enum & Quantity> doub return values; } + private LfNetwork network; + private LfBranch branch; private LfBus bus1; @@ -116,11 +118,20 @@ void setUp() { Mockito.doReturn(B_2).when(piModel).getB2(); Mockito.doReturn(KSI).when(piModel).getKsi(); Mockito.doReturn(R_1).when(piModel).getR1(); + Mockito.doReturn(A_1).when(piModel).getA1(); bus1 = Mockito.mock(LfBus.class, ANSWER); bus2 = Mockito.mock(LfBus.class, ANSWER); Mockito.doReturn(0).when(bus1).getNum(); Mockito.doReturn(1).when(bus2).getNum(); + + network = Mockito.mock(LfNetwork.class); + Mockito.doReturn(List.of(bus1, bus2)).when(network).getBuses(); + Mockito.doReturn(List.of(branch)).when(network).getBranches(); + Mockito.doReturn(bus1).when(branch).getBus1(); + Mockito.doReturn(bus2).when(branch).getBus2(); + Mockito.doReturn(true).when(branch).isPhaseController(); + Mockito.doReturn(true).when(branch).isVoltageController(); } @Test @@ -143,12 +154,21 @@ void branchTest() { a1Var.setRow(5); unknownVar.setRow(6); - var sv = new StateVector(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); - - LfNetwork lfNetwork = Mockito.mock(LfNetwork.class); EquationSystem equationSystem = new EquationSystem<>(); - AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem); + var sv = equationSystem.getStateVector(); + sv.set(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); + AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + AcBusVector busVector = networkVector.getBusVector(); AcBranchVector branchVector = networkVector.getBranchVector(); + busVector.vRow[0] = v1Var.getRow(); + busVector.vRow[1] = v2Var.getRow(); + busVector.phRow[0] = ph1Var.getRow(); + busVector.phRow[1] = ph2Var.getRow(); + branchVector.a1Row[0] = a1Var.getRow(); + branchVector.r1Row[0] = r1Var.getRow(); + networkVector.updateBranchVariables(); + networkVector.updateNetwork(); // closed branch equations assertArrayEquals(new double[] {41.78173051479356, 48.66261692116701, 138.21343172859858, 29.31710523088579, -138.21343172859858, 54.62161149356045, 138.21343172859858, Double.NaN, 270.81476537421185}, diff --git a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java index 8e3d5b8337..e56a5bc911 100644 --- a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java +++ b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java @@ -71,7 +71,8 @@ void setterTest() { lfSwitch.getPiModel().setX(LfNetworkParameters.LOW_IMPEDANCE_THRESHOLD_DEFAULT_VALUE); EquationSystem equationSystem = new EquationSystem<>(); - AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem); + AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); + AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem, creationParameters); VariableSet variableSet = new VariableSet<>(); EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); From 3257ef1d62021008f0f165bd11e59dc718c2ad55 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 19:55:26 +0200 Subject: [PATCH 04/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../powsybl/openloadflow/ac/equations/AcNetworkVector.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 820f5bb3d6..48e1580a02 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -86,11 +86,11 @@ public void updateVariables() { break; case BRANCH_ALPHA1: - branchVector.a1Row[num] = branchVector.deriveA1[num] ? row : -1; + branchVector.a1Row[num] = branchVector.deriveA1[num] ? row : -1; break; case BRANCH_RHO1: - branchVector.r1Row[num] = branchVector.deriveR1[num] ? row : -1; + branchVector.r1Row[num] = branchVector.deriveR1[num] ? row : -1; break; default: From 759294c168bf813e84016ce67c91a57378c4fc22 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 20:21:30 +0200 Subject: [PATCH 05/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcNetworkVector.java | 42 +++++++++++++++++++ ...edBranchSide1ReactiveFlowEquationTerm.java | 15 ++++--- ...osedBranchSide2ActiveFlowEquationTerm.java | 15 ++++--- ...edBranchSide2ReactiveFlowEquationTerm.java | 15 ++++--- 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 48e1580a02..2ad5e71cf2 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -245,6 +245,22 @@ public void updateNetwork() { v2, sinTheta1); + branchVector.dq1da1[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.dq1da1( + branchVector.y[branchNum], + v1, + r1, + v2, + sinTheta1); + + branchVector.dq1dr1[branchNum] = ClosedBranchSide1ReactiveFlowEquationTerm.dq1dr1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.b1[branchNum], + v1, + r1, + v2, + cosTheta1); + // i1 branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / (v1 * SQRT3 / 1000); @@ -289,6 +305,19 @@ public void updateNetwork() { v2, cosTheta2); + branchVector.dp2da1[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.dp2da1( + branchVector.y[branchNum], + v1, + r1, + v2, + cosTheta2); + + branchVector.dp2dr1[branchNum] = ClosedBranchSide2ActiveFlowEquationTerm.dp2dr1( + branchVector.y[branchNum], + v1, + v2, + sinTheta2); + // q2 branchVector.q2[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.q2( @@ -329,6 +358,19 @@ public void updateNetwork() { v2, sinTheta2); + branchVector.dq2da1[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.dq2da1( + branchVector.y[branchNum], + v1, + r1, + v2, + sinTheta2); + + branchVector.dq2dr1[branchNum] = ClosedBranchSide2ReactiveFlowEquationTerm.dq2dr1( + branchVector.y[branchNum], + v1, + v2, + cosTheta2); + // i2 branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / (v2 * SQRT3 / 1000); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index aa9ab8e4a3..307faa9245 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -75,25 +75,24 @@ public static double dq1dr1(double y, double cosKsi, double b1, double v1, doubl @Override public double eval() { - return q1(y, FastMath.cos(ksi), b1, v1(), r1(), v2(), FastMath.cos(theta1(ksi, ph1(), a1(), ph2()))); + return branchVector.q1[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double theta = theta1(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return dq1dv1(y, FastMath.cos(ksi), b1, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dq1dv1[num]; } else if (variable.equals(v2Var)) { - return dq1dv2(y, v1(), r1(), FastMath.cos(theta)); + return branchVector.dq1dv2[num]; } else if (variable.equals(ph1Var)) { - return dq1dph1(y, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dq1dph1[num]; } else if (variable.equals(ph2Var)) { - return dq1dph2(y, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dq1dph2[num]; } else if (variable.equals(a1Var)) { - return dq1da1(y, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dq1da1[num]; } else if (variable.equals(r1Var)) { - return dq1dr1(y, FastMath.cos(ksi), b1, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dq1dr1[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index 61f956191b..231c6c0d21 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -72,25 +72,24 @@ public static double dp2dr1(double y, double v1, double v2, double sinTheta) { @Override public double eval() { - return p2(y, FastMath.sin(ksi), g2, v1(), r1(), v2(), FastMath.sin(theta2(ksi, ph1(), a1(), ph2()))); + return branchVector.p2[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double theta = theta2(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return dp2dv1(y, r1(), v2(), FastMath.sin(theta)); + return branchVector.dp2dv1[num]; } else if (variable.equals(v2Var)) { - return dp2dv2(y, FastMath.sin(ksi), g2, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dp2dv2[num]; } else if (variable.equals(ph1Var)) { - return dp2dph1(y, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dp2dph1[num]; } else if (variable.equals(ph2Var)) { - return dp2dph2(y, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dp2dph2[num]; } else if (variable.equals(a1Var)) { - return dp2da1(y, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dp2da1[num]; } else if (variable.equals(r1Var)) { - return dp2dr1(y, v1(), v2(), FastMath.sin(theta)); + return branchVector.dp2dr1[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index 17d0ef4c9e..efcae663ec 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -72,25 +72,24 @@ public static double dq2dr1(double y, double v1, double v2, double cosTheta) { @Override public double eval() { - return q2(y, FastMath.cos(ksi), b2, v1(), r1(), v2(), FastMath.cos(theta2(ksi, ph1(), a1(), ph2()))); + return branchVector.q2[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double theta = theta2(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return dq2dv1(y, r1(), v2(), FastMath.cos(theta)); + return branchVector.dq2dv1[num]; } else if (variable.equals(v2Var)) { - return dq2dv2(y, FastMath.cos(ksi), b2, v1(), r1(), v2(), FastMath.cos(theta)); + return branchVector.dq2dv2[num]; } else if (variable.equals(ph1Var)) { - return dq2dph1(y, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dq2dph1[num]; } else if (variable.equals(ph2Var)) { - return dq2dph2(y, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dq2dph2[num]; } else if (variable.equals(a1Var)) { - return dq2da1(y, v1(), r1(), v2(), FastMath.sin(theta)); + return branchVector.dq2da1[num]; } else if (variable.equals(r1Var)) { - return dq2dr1(y, v1(), v2(), FastMath.cos(theta)); + return branchVector.dq2dr1[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } From 2177030eaaff1804b6cb1c37bfd64bfd188ce0e6 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 21:14:36 +0200 Subject: [PATCH 06/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcNetworkVector.java | 38 +++++++++++++++ ...OpenBranchSide1ActiveFlowEquationTerm.java | 9 +--- ...enBranchSide1ReactiveFlowEquationTerm.java | 9 +--- ...OpenBranchSide2ActiveFlowEquationTerm.java | 13 +----- ...enBranchSide2ReactiveFlowEquationTerm.java | 13 +----- .../powsybl/openloadflow/EquationsTest.java | 46 ++++++++++++------- 6 files changed, 76 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 2ad5e71cf2..689bb67b86 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -389,6 +389,16 @@ public void updateNetwork() { v1, r1); + branchVector.dp1dv1[branchNum] = OpenBranchSide2ActiveFlowEquationTerm.dp1dv1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + branchVector.g2[branchNum], + branchVector.b2[branchNum], + v1, + r1); + branchVector.q1[branchNum] = OpenBranchSide2ReactiveFlowEquationTerm.q1( branchVector.y[branchNum], branchVector.cosKsi[branchNum], @@ -399,6 +409,16 @@ public void updateNetwork() { v1, r1); + branchVector.dq1dv1[branchNum] = OpenBranchSide2ReactiveFlowEquationTerm.dq1dv1( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.b1[branchNum], + branchVector.g2[branchNum], + branchVector.b2[branchNum], + v1, + r1); + branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / (v1 * SQRT3 / 1000); } else if (branchVector.bus2Num[branchNum] != -1) { double v2 = state[branchVector.v2Row[branchNum]]; @@ -412,6 +432,15 @@ public void updateNetwork() { branchVector.g2[branchNum], v2); + branchVector.dp2dv2[branchNum] = OpenBranchSide1ActiveFlowEquationTerm.dp2dv2( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + branchVector.b1[branchNum], + branchVector.g2[branchNum], + v2); + branchVector.q2[branchNum] = OpenBranchSide1ReactiveFlowEquationTerm.q2( branchVector.y[branchNum], branchVector.cosKsi[branchNum], @@ -421,6 +450,15 @@ public void updateNetwork() { branchVector.b2[branchNum], v2); + branchVector.dq2dv2[branchNum] = OpenBranchSide1ReactiveFlowEquationTerm.dq2dv2( + branchVector.y[branchNum], + branchVector.cosKsi[branchNum], + branchVector.sinKsi[branchNum], + branchVector.g1[branchNum], + branchVector.b1[branchNum], + branchVector.b2[branchNum], + v2); + branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / (v2 * SQRT3 / 1000); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java index be33223235..72b3307145 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java @@ -10,7 +10,6 @@ import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.network.LfBranch; import com.powsybl.openloadflow.network.LfBus; -import net.jafama.FastMath; import java.util.Objects; @@ -29,10 +28,6 @@ public OpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcBran v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); } - private double v2() { - return sv.get(v2Var.getRow()); - } - public static double p2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { double shunt = shunt(y, cosKsi, sinKsi, g1, b1); return R2 * R2 * v2 * v2 * (g2 + y * y * g1 / shunt + (b1 * b1 + g1 * g1) * y * sinKsi / shunt); @@ -45,14 +40,14 @@ public static double dp2dv2(double y, double cosKsi, double sinKsi, double g1, d @Override public double eval() { - return p2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, v2()); + return branchVector.p2[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v2Var)) { - return dp2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, v2()); + return branchVector.dp2dv2[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java index a2c71f0605..47b53617ef 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java @@ -10,7 +10,6 @@ import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.network.LfBranch; import com.powsybl.openloadflow.network.LfBus; -import net.jafama.FastMath; import java.util.Objects; @@ -29,10 +28,6 @@ public OpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcBr v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); } - private double v2() { - return sv.get(v2Var.getRow()); - } - public static double q2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { double shunt = shunt(y, cosKsi, sinKsi, g1, b1); return -R2 * R2 * v2 * v2 * (b2 + y * y * b1 / shunt - (b1 * b1 + g1 * g1) * y * cosKsi / shunt); @@ -45,14 +40,14 @@ public static double dq2dv2(double y, double cosKsi, double sinKsi, double g1, d @Override public double eval() { - return q2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, b2, v2()); + return branchVector.q2[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v2Var)) { - return dq2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, b2, v2()); + return branchVector.dq2dv2[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java index 158c444cce..2ace28ac1e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java @@ -10,7 +10,6 @@ import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.network.LfBranch; import com.powsybl.openloadflow.network.LfBus; -import net.jafama.FastMath; import java.util.Objects; @@ -27,14 +26,6 @@ public OpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcBran v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); } - private double v1() { - return sv.get(v1Var.getRow()); - } - - private double r1() { - return element.getPiModel().getR1(); - } - public static double p1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { double shunt = shunt(y, cosKsi, sinKsi, g2, b2); return r1 * r1 * v1 * v1 * (g1 + y * y * g2 / shunt + (b2 * b2 + g2 * g2) * y * sinKsi / shunt); @@ -47,14 +38,14 @@ public static double dp1dv1(double y, double cosKsi, double sinKsi, double g1, d @Override public double eval() { - return p1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, g2, b2, v1(), r1()); + return branchVector.p1[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return dp1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, g2, b2, v1(), r1()); + return branchVector.dp1dv1[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java index 570e99693a..3ed108f6ff 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java @@ -10,7 +10,6 @@ import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.network.LfBranch; import com.powsybl.openloadflow.network.LfBus; -import net.jafama.FastMath; import java.util.Objects; @@ -27,14 +26,6 @@ public OpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcBr v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); } - private double v1() { - return sv.get(v1Var.getRow()); - } - - private double r1() { - return element.getPiModel().getR1(); - } - public static double q1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { double shunt = shunt(y, cosKsi, sinKsi, g2, b2); return -r1 * r1 * v1 * v1 * (b1 + y * y * b2 / shunt - (b2 * b2 + g2 * g2) * y * cosKsi / shunt); @@ -47,14 +38,14 @@ public static double dq1dv1(double y, double cosKsi, double sinKsi, double b1, d @Override public double eval() { - return q1(y, FastMath.cos(ksi), FastMath.sin(ksi), b1, g2, b2, v1(), r1()); + return branchVector.q1[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return dq1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), b1, g2, b2, v1(), r1()); + return branchVector.dq1dv1[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 631c6830bf..76634d80f5 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -128,10 +128,31 @@ void setUp() { network = Mockito.mock(LfNetwork.class); Mockito.doReturn(List.of(bus1, bus2)).when(network).getBuses(); Mockito.doReturn(List.of(branch)).when(network).getBranches(); + } + + private AcBranchVector createBranchVector(LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, + EquationSystem equationSystem, + Variable v1Var, Variable v2Var, + Variable ph1Var, Variable ph2Var, + Variable a1Var, Variable r1Var) { + Mockito.doReturn(deriveA1).when(branch).isPhaseController(); + Mockito.doReturn(deriveR1).when(branch).isVoltageController(); Mockito.doReturn(bus1).when(branch).getBus1(); Mockito.doReturn(bus2).when(branch).getBus2(); - Mockito.doReturn(true).when(branch).isPhaseController(); - Mockito.doReturn(true).when(branch).isVoltageController(); + + AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + AcBusVector busVector = networkVector.getBusVector(); + AcBranchVector branchVector = networkVector.getBranchVector(); + busVector.vRow[0] = v1Var.getRow(); + busVector.vRow[1] = v2Var.getRow(); + busVector.phRow[0] = ph1Var.getRow(); + busVector.phRow[1] = ph2Var.getRow(); + branchVector.a1Row[0] = a1Var.getRow(); + branchVector.r1Row[0] = r1Var.getRow(); + networkVector.updateBranchVariables(); + networkVector.updateNetwork(); + return networkVector.getBranchVector(); } @Test @@ -157,20 +178,9 @@ void branchTest() { EquationSystem equationSystem = new EquationSystem<>(); var sv = equationSystem.getStateVector(); sv.set(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); - AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); - AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); - AcBusVector busVector = networkVector.getBusVector(); - AcBranchVector branchVector = networkVector.getBranchVector(); - busVector.vRow[0] = v1Var.getRow(); - busVector.vRow[1] = v2Var.getRow(); - busVector.phRow[0] = ph1Var.getRow(); - busVector.phRow[1] = ph2Var.getRow(); - branchVector.a1Row[0] = a1Var.getRow(); - branchVector.r1Row[0] = r1Var.getRow(); - networkVector.updateBranchVariables(); - networkVector.updateNetwork(); // closed branch equations + AcBranchVector branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {41.78173051479356, 48.66261692116701, 138.21343172859858, 29.31710523088579, -138.21343172859858, 54.62161149356045, 138.21343172859858, Double.NaN, 270.81476537421185}, eval(new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-3.500079625302254, 122.46444997806617, 31.42440177840898, -128.9449438332101, -31.42440177840898, 137.46086897280827, 31.42440177840898, Double.NaN, 162.40477689607334}, @@ -185,19 +195,23 @@ void branchTest() { eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); // open branch equations + branchVector = createBranchVector(null, bus2, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {0.1717595025847833, Double.NaN, Double.NaN, 0.3204828812456483, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.37520249405559764, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {0.15652310047954035, Double.NaN, Double.NaN, 0.2920535601715773, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + + branchVector = createBranchVector(bus1, null, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); + assertArrayEquals(new double[] {0.15639470221220209, Double.NaN, Double.NaN, 0.2919337476186018, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide2ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {-0.331495628053771, Double.NaN, Double.NaN, -0.6185315653587614, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + assertArrayEquals(new double[] {-0.33122369717493005, Double.NaN, Double.NaN, -0.6182778179094991, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.3420075216110214, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); // assert current equation is consistent with active and reactive power ones var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); p1Eq.setStateVector(sv); From 031156568858a0489e78695f1eca40a2d6039c08 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 21:19:05 +0200 Subject: [PATCH 07/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/ac/equations/AcNetworkVector.java | 10 ++++------ .../ClosedBranchSide1CurrentMagnitudeEquationTerm.java | 2 +- .../OpenBranchSide1CurrentMagnitudeEquationTerm.java | 2 +- .../java/com/powsybl/openloadflow/EquationsTest.java | 4 ++-- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 689bb67b86..c15106cc0e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -26,8 +26,6 @@ public class AcNetworkVector extends AbstractLfNetworkListener private static final Logger LOGGER = LoggerFactory.getLogger(AcNetworkVector.class); - private static final double SQRT3 = FastMath.sqrt(3); - private final LfNetwork network; private final EquationSystem equationSystem; private final AcBusVector busVector; @@ -263,7 +261,7 @@ public void updateNetwork() { // i1 - branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / (v1 * SQRT3 / 1000); + branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / v1; // p2 @@ -373,7 +371,7 @@ public void updateNetwork() { // i2 - branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / (v2 * SQRT3 / 1000); + branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / v2; } else if (branchVector.bus1Num[branchNum] != -1) { double v1 = state[branchVector.v1Row[branchNum]]; double r1 = branchVector.r1Row[branchNum] != -1 ? state[branchVector.r1Row[branchNum]] @@ -419,7 +417,7 @@ public void updateNetwork() { v1, r1); - branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / (v1 * SQRT3 / 1000); + branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / v1; } else if (branchVector.bus2Num[branchNum] != -1) { double v2 = state[branchVector.v2Row[branchNum]]; @@ -459,7 +457,7 @@ public void updateNetwork() { branchVector.b2[branchNum], v2); - branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / (v2 * SQRT3 / 1000); + branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / v2; } } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index b8ed7ebfe8..6d71cbc6e9 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -133,7 +133,7 @@ private static double di1da1(double y, double ksi, double g1, double b1, double @Override public double eval() { - return i1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); + return branchVector.i1[num]; } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java index 897c96af45..727957a70c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java @@ -80,7 +80,7 @@ private static double di2dv2(double y, double cosKsi, double sinKsi, double g1, @Override public double eval() { - return i2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v2(), ph2()); + return branchVector.i2[num]; } @Override diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 76634d80f5..54a900f33f 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -185,7 +185,7 @@ void branchTest() { eval(new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-3.500079625302254, 122.46444997806617, 31.42440177840898, -128.9449438332101, -31.42440177840898, 137.46086897280827, 31.42440177840898, Double.NaN, 162.40477689607334}, eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); - assertArrayEquals(new double[] {39.13246485286217, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, + assertArrayEquals(new double[] {39.13246485286219, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-40.6365773800554, -48.52391742324069, -131.8614376204652, -27.319027760225953, 131.8614376204652, -54.4659275092331, -131.8614376204652, Double.NaN, -262.1703103131649}, eval(new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); @@ -200,7 +200,7 @@ void branchTest() { eval(new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {0.37520249405559764, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + assertArrayEquals(new double[] {0.3752024940555977, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); branchVector = createBranchVector(bus1, null, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); From c463e66324a594614150b6a9125f4b501513e009 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 21:36:32 +0200 Subject: [PATCH 08/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AbstractBranchAcFlowEquationTerm.java | 18 ------------------ ...losedBranchSide1ActiveFlowEquationTerm.java | 3 +++ ...ranchSide1CurrentMagnitudeEquationTerm.java | 8 ++++++++ ...sedBranchSide1ReactiveFlowEquationTerm.java | 4 ++++ ...losedBranchSide2ActiveFlowEquationTerm.java | 3 +++ ...ranchSide2CurrentMagnitudeEquationTerm.java | 10 +++++++++- ...sedBranchSide2ReactiveFlowEquationTerm.java | 3 +++ ...ranchSide1CurrentMagnitudeEquationTerm.java | 6 ++++++ ...ranchSide2CurrentMagnitudeEquationTerm.java | 8 +++++++- .../powsybl/openloadflow/EquationsTest.java | 4 ++-- 10 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java index e042191609..48028d76d2 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java @@ -8,7 +8,6 @@ import com.powsybl.openloadflow.equations.AbstractElementEquationTerm; import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.PiModel; import java.util.Objects; @@ -20,26 +19,9 @@ abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationT protected final AcBranchVector branchVector; protected final int num; - protected final double b1; - protected final double b2; - protected final double g1; - protected final double g2; - protected final double y; - protected final double ksi; - protected AbstractBranchAcFlowEquationTerm(LfBranch branch, AcBranchVector branchVector) { super(branch); this.branchVector = Objects.requireNonNull(branchVector); this.num = branch.getNum(); - PiModel piModel = branch.getPiModel(); - if (piModel.getR() == 0 && piModel.getX() == 0) { - throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); - } - b1 = piModel.getB1(); - b2 = piModel.getB2(); - g1 = piModel.getG1(); - g2 = piModel.getG2(); - y = piModel.getY(); - ksi = piModel.getKsi(); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index 7aec4f4d25..425db80464 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -28,6 +28,9 @@ public ClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBu } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index 6d71cbc6e9..0c14973a8c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -33,6 +33,10 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 if (dr1 != 0) { throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); } + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; double v1 = v1(); double ph1 = ph1(); double r1 = r1(); @@ -139,6 +143,10 @@ public double eval() { @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; if (variable.equals(v1Var)) { return di1dv1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); } else if (variable.equals(v2Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index 307faa9245..d20ae3f0fd 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -28,6 +28,10 @@ public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, Lf } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index 231c6c0d21..e92026f6c7 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -28,6 +28,9 @@ public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBu } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g2 = branchVector.g2[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java index 7bc89699ec..641d401466 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java @@ -33,6 +33,10 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 if (dr1 != 0) { throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); } + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g2 = branchVector.g2[num]; + double b2 = branchVector.b2[num]; double v1 = v1(); double ph1 = ph1(); double r1 = r1(); @@ -133,12 +137,16 @@ private static double di2da1(double y, double ksi, double g2, double b2, double @Override public double eval() { - return i2(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); + return branchVector.i2[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g2 = branchVector.g2[num]; + double b2 = branchVector.b2[num]; if (variable.equals(v1Var)) { return di2dv1(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); } else if (variable.equals(v2Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index efcae663ec..d7eef6466b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -28,6 +28,9 @@ public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, Lf } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double b2 = branchVector.b2[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java index 727957a70c..2fdaee0695 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java @@ -86,6 +86,12 @@ public double eval() { @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; + double g2 = branchVector.g2[num]; + double b2 = branchVector.b2[num]; if (variable.equals(v2Var)) { return di2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v2(), ph2()); } else if (variable.equals(ph2Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java index f67d49ebe5..0faa4a982a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java @@ -87,12 +87,18 @@ private static double di1dv1(double y, double cosKsi, double sinKsi, double g1, @Override public double eval() { - return i1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v1(), ph1(), r1()); + return branchVector.i1[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; + double b2 = branchVector.b2[num]; + double g2 = branchVector.g2[num]; if (variable.equals(v1Var)) { return di1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v1(), ph1(), r1()); } else if (variable.equals(ph1Var) || variable.equals(r1Var)) { diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 54a900f33f..a8bc5694a7 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -191,7 +191,7 @@ void branchTest() { eval(new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {16.04980301110306, -123.06939783256767, 51.99045110393844, 152.96594042215764, -51.99045110393844, -138.1398958886022, 51.99045110393844, Double.NaN, -56.2529021950738}, eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); - assertArrayEquals(new double[] {40.7613721648136, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, + assertArrayEquals(new double[] {40.76137216481359, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); // open branch equations @@ -208,7 +208,7 @@ void branchTest() { eval(new OpenBranchSide2ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.33122369717493005, Double.NaN, Double.NaN, -0.6182778179094991, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {0.3420075216110214, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + assertArrayEquals(new double[] {0.34186721585930596, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); From daa5d5669d2d59486dfc0f613d50c7a6dbf2ab2d Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 21:55:42 +0200 Subject: [PATCH 09/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/ac/equations/AcNetworkVector.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index c15106cc0e..8c199e66be 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -30,6 +30,7 @@ public class AcNetworkVector extends AbstractLfNetworkListener private final EquationSystem equationSystem; private final AcBusVector busVector; private final AcBranchVector branchVector; + private boolean variablesInvalid = true; public AcNetworkVector(LfNetwork network, EquationSystem equationSystem, AcEquationSystemCreationParameters creationParameters) { @@ -60,6 +61,10 @@ public void startListening() { } public void updateVariables() { + if (!variablesInvalid) { + return; + } + Stopwatch stopwatch = Stopwatch.createStarted(); Arrays.fill(busVector.vRow, -1); @@ -99,6 +104,8 @@ public void updateVariables() { stopwatch.stop(); LOGGER.info("AC variable vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); + + variablesInvalid = false; } public void updateBranchVariables() { @@ -482,8 +489,7 @@ public void onTapPositionChange(LfBranch branch, int oldPosition, int newPositio @Override public void onVariableChange(Variable variable, ChangeType changeType) { - updateVariables(); - // TODO also update state? + variablesInvalid = true; } @Override @@ -498,6 +504,7 @@ public void onEquationTermChange(EquationTerm te @Override public void onStateUpdate() { + updateVariables(); updateNetwork(); } } From 7436aa91f67d1206114c133b64dc33db9c2f9fa0 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 27 Apr 2023 22:17:58 +0200 Subject: [PATCH 10/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../powsybl/openloadflow/ac/equations/AcBranchVector.java | 8 ++++---- .../openloadflow/ac/equations/AcNetworkVector.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java index 5e52876689..16ecdf9cce 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java @@ -32,7 +32,7 @@ public class AcBranchVector { final double[] r1; final double[] a1; - final int[] status; + final boolean[] disabled; final boolean[] deriveA1; final boolean[] deriveR1; @@ -94,7 +94,7 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter b2 = new double[size]; r1 = new double[size]; a1 = new double[size]; - status = new int[size]; + disabled = new boolean[size]; deriveA1 = new boolean[size]; deriveR1 = new boolean[size]; @@ -161,13 +161,13 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter g2[i] = piModel.getG2(); r1[i] = piModel.getR1(); a1[i] = piModel.getA1(); - status[i] = branch.isDisabled() ? 0 : 1; + disabled[i] = branch.isDisabled(); deriveA1[i] = AcEquationSystemCreator.isDeriveA1(branch, creationParameters); deriveR1[i] = AcEquationSystemCreator.isDeriveR1(branch); } } public int getSize() { - return status.length; + return disabled.length; } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 8c199e66be..069ca07192 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -127,7 +127,7 @@ public void updateNetwork() { double[] state = equationSystem.getStateVector().get(); var w = new DoubleWrapper(); for (int branchNum = 0; branchNum < branchVector.getSize(); branchNum++) { - if (branchVector.status[branchNum] == 1) { + if (!branchVector.disabled[branchNum]) { if (branchVector.bus1Num[branchNum] != -1 && branchVector.bus2Num[branchNum] != -1) { double ph1 = state[branchVector.ph1Row[branchNum]]; double ph2 = state[branchVector.ph2Row[branchNum]]; @@ -476,7 +476,7 @@ public void updateNetwork() { @Override public void onDisableChange(LfElement element, boolean disabled) { if (element.getType() == ElementType.BRANCH) { - branchVector.status[element.getNum()] = disabled ? 0 : 1; + branchVector.disabled[element.getNum()] = disabled; } } From a1107efbf269379e34dd88a4481805ec6695099f Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Fri, 28 Apr 2023 18:20:28 +0200 Subject: [PATCH 11/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AcEquationSystemCreationContext.java | 34 ++++++++ .../ac/equations/AcEquationSystemCreator.java | 86 ++++++++++--------- .../ac/equations/AcEquationSystemUpdater.java | 2 +- 3 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java new file mode 100644 index 0000000000..c2b87984f9 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java @@ -0,0 +1,34 @@ +/** + * 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.ac.equations; + +import com.powsybl.openloadflow.equations.EquationSystem; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian + */ +public class AcEquationSystemCreationContext { + + private final EquationSystem equationSystem; + + private final AcNetworkVector networkVector; + + public AcEquationSystemCreationContext(EquationSystem equationSystem, AcNetworkVector networkVector) { + this.equationSystem = Objects.requireNonNull(equationSystem); + this.networkVector = Objects.requireNonNull(networkVector); + } + + public EquationSystem getEquationSystem() { + return equationSystem; + } + + public AcNetworkVector getNetworkVector() { + return networkVector; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 955ee3a43a..8f8b6759de 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -11,7 +11,10 @@ import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.network.TransformerPhaseControl.Mode; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -60,11 +63,11 @@ private void createBusEquation(LfBus bus, createShuntEquations(bus, equationSystem); } - private void createVoltageControlEquations(AcBranchVector branchVector, EquationSystem equationSystem) { + private void createVoltageControlEquations(AcEquationSystemCreationContext creationContext) { for (LfBus bus : network.getBuses()) { - createGeneratorVoltageControlEquations(bus, branchVector, equationSystem); - createTransformerVoltageControlEquations(bus, equationSystem); - createShuntVoltageControlEquations(bus, equationSystem); + createGeneratorVoltageControlEquations(bus, creationContext); + createTransformerVoltageControlEquations(bus, creationContext.getEquationSystem()); + createShuntVoltageControlEquations(bus, creationContext.getEquationSystem()); } } @@ -74,24 +77,23 @@ private void createBusesEquations(EquationSystem } } - private void createGeneratorVoltageControlEquations(LfBus bus, AcBranchVector branchVector, - EquationSystem equationSystem) { + private void createGeneratorVoltageControlEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { bus.getGeneratorVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> { if (bus.isGeneratorVoltageControlled()) { if (voltageControl.isLocalControl()) { - createGeneratorLocalVoltageControlEquation(bus, branchVector, equationSystem); + createGeneratorLocalVoltageControlEquation(bus, creationContext); } else { - createGeneratorRemoteVoltageControlEquations(branchVector, voltageControl, equationSystem); + createGeneratorRemoteVoltageControlEquations(voltageControl, creationContext); } - updateGeneratorVoltageControl(voltageControl, equationSystem); + updateGeneratorVoltageControl(voltageControl, creationContext.getEquationSystem()); } }); } - private void createGeneratorLocalVoltageControlEquation(LfBus bus, AcBranchVector branchVector, - EquationSystem equationSystem) { + private void createGeneratorLocalVoltageControlEquation(LfBus bus, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); if (bus.hasGeneratorsWithSlope()) { // take first generator with slope: network loading ensures that there's only one generator with slope double slope = bus.getGeneratorsControllingVoltageWithSlope().get(0).getSlope(); @@ -100,7 +102,7 @@ private void createGeneratorLocalVoltageControlEquation(LfBus bus, AcBranchVecto // equation is: V + slope * qSVC = targetV // which is modeled here with: V + slope * (sum_branch qBranch) = TargetV - slope * qLoads + slope * qGenerators equationSystem.getEquation(bus.getNum(), AcEquationType.BUS_TARGET_V).orElseThrow() - .addTerms(createReactiveTerms(bus, branchVector, equationSystem.getVariableSet(), creationParameters) + .addTerms(createReactiveTerms(bus, creationContext, creationParameters) .stream() .map(term -> term.multiply(slope)) .collect(Collectors.toList())); @@ -151,7 +153,8 @@ private static void createShuntEquations(LfBus bus, EquationSystem createShuntEquation(shunt, bus, equationSystem, false)); } - private static void createReactivePowerDistributionEquations(AcBranchVector branchVector, GeneratorVoltageControl voltageControl, EquationSystem equationSystem, + private static void createReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, + AcEquationSystemCreationContext creationContext, AcEquationSystemCreationParameters creationParameters) { List controllerBuses = voltageControl.getMergedControllerElements(); for (LfBus controllerBus : controllerBuses) { @@ -160,13 +163,13 @@ private static void createReactivePowerDistributionEquations(AcBranchVector bran // 0 = qPercent_i * sum_j(q_j) - q_i // which can be rewritten in a more simple way // 0 = (qPercent_i - 1) * q_i + qPercent_i * sum_j(q_j) where j are all the voltage controller buses except i - Equation zero = equationSystem.createEquation(controllerBus, AcEquationType.DISTR_Q) - .addTerms(createReactiveTerms(controllerBus, branchVector, equationSystem.getVariableSet(), creationParameters).stream() + Equation zero = creationContext.getEquationSystem().createEquation(controllerBus, AcEquationType.DISTR_Q) + .addTerms(createReactiveTerms(controllerBus, creationContext, creationParameters).stream() .map(term -> term.multiply(() -> controllerBus.getRemoteVoltageControlReactivePercent() - 1)) .collect(Collectors.toList())); for (LfBus otherControllerBus : controllerBuses) { if (otherControllerBus != controllerBus) { - zero.addTerms(createReactiveTerms(otherControllerBus, branchVector, equationSystem.getVariableSet(), creationParameters).stream() + zero.addTerms(createReactiveTerms(otherControllerBus, creationContext, creationParameters).stream() .map(term -> term.multiply(controllerBus::getRemoteVoltageControlReactivePercent)) .collect(Collectors.toList())); } @@ -174,26 +177,27 @@ private static void createReactivePowerDistributionEquations(AcBranchVector bran } } - public static void recreateReactivePowerDistributionEquations(AcBranchVector branchVector, GeneratorVoltageControl voltageControl, - EquationSystem equationSystem, + public static void recreateReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, + AcEquationSystemCreationContext creationContext, AcEquationSystemCreationParameters parameters) { + var equationSystem = creationContext.getEquationSystem(); for (LfBus controllerBus : voltageControl.getMergedControllerElements()) { equationSystem.removeEquation(controllerBus.getNum(), AcEquationType.DISTR_Q); } if (!voltageControl.isLocalControl()) { - createReactivePowerDistributionEquations(branchVector, voltageControl, equationSystem, parameters); + createReactivePowerDistributionEquations(voltageControl, creationContext, parameters); } updateGeneratorVoltageControl(voltageControl, equationSystem); } - private void createGeneratorRemoteVoltageControlEquations(AcBranchVector branchVector, GeneratorVoltageControl voltageControl, - EquationSystem equationSystem) { + private void createGeneratorRemoteVoltageControlEquations(GeneratorVoltageControl voltageControl, + AcEquationSystemCreationContext creationContext) { for (LfBus controllerBus : voltageControl.getMergedControllerElements()) { - equationSystem.createEquation(controllerBus, AcEquationType.BUS_TARGET_Q); + creationContext.getEquationSystem().createEquation(controllerBus, AcEquationType.BUS_TARGET_Q); } // create reactive power distribution equations at voltage controller buses - createReactivePowerDistributionEquations(branchVector, voltageControl, equationSystem, creationParameters); + createReactivePowerDistributionEquations(voltageControl, creationContext, creationParameters); } static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, @@ -282,9 +286,11 @@ static void updateRemoteGeneratorVoltageControlEquations(GeneratorVoltageControl updateRemoteVoltageControlEquations(voltageControl, equationSystem, AcEquationType.DISTR_Q, AcEquationType.BUS_TARGET_Q); } - private static List> createReactiveTerms(LfBus controllerBus, AcBranchVector branchVector, - VariableSet variableSet, + private static List> createReactiveTerms(LfBus controllerBus, + AcEquationSystemCreationContext creationContext, AcEquationSystemCreationParameters creationParameters) { + var variableSet = creationContext.getEquationSystem().getVariableSet(); + var branchVector = creationContext.getNetworkVector().getBranchVector(); List> terms = new ArrayList<>(); for (LfBranch branch : controllerBus.getBranches()) { EquationTerm q; @@ -600,8 +606,9 @@ static boolean isDeriveR1(LfBranch branch) { return branch.isVoltageController(); } - private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, - EquationSystem equationSystem) { + private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); + var branchVector = creationContext.getNetworkVector().getBranchVector(); EquationTerm p1 = null; EquationTerm q1 = null; EquationTerm p2 = null; @@ -691,19 +698,18 @@ private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem equationSystem) { + private void createBranchEquations(LfBranch branch, AcEquationSystemCreationContext creationContext) { // create zero and non zero impedance branch equations if (branch.isZeroImpedance(LoadFlowModel.AC)) { - createNonImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem, branch.isSpanningTreeEdge(LoadFlowModel.AC)); + createNonImpedantBranch(branch, branch.getBus1(), branch.getBus2(), creationContext.getEquationSystem(), branch.isSpanningTreeEdge(LoadFlowModel.AC)); } else { - createImpedantBranch(branch, branch.getBus1(), branch.getBus2(), branchVector, equationSystem); + createImpedantBranch(branch, branch.getBus1(), branch.getBus2(), creationContext); } } - private void createBranchesEquations(AcBranchVector branchVector, EquationSystem equationSystem) { + private void createBranchesEquations(AcEquationSystemCreationContext creationContext) { for (LfBranch branch : network.getBranches()) { - createBranchEquations(branch, branchVector, equationSystem); + createBranchEquations(branch, creationContext); } } @@ -738,7 +744,9 @@ private List> createActiveInjection return terms; } - private void createMultipleSlackBusesEquations(AcBranchVector branchVector, EquationSystem equationSystem) { + private void createMultipleSlackBusesEquations(AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); + var branchVector = creationContext.getNetworkVector().getBranchVector(); List slackBuses = network.getSlackBuses(); if (slackBuses.size() > 1) { LfBus firstSlackBus = slackBuses.get(0); @@ -757,20 +765,20 @@ private void createMultipleSlackBusesEquations(AcBranchVector branchVector, Equa } public EquationSystem create() { - EquationSystem equationSystem = new EquationSystem<>(); AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + AcEquationSystemCreationContext creationContext = new AcEquationSystemCreationContext(equationSystem, networkVector); createBusesEquations(equationSystem); - createMultipleSlackBusesEquations(networkVector.getBranchVector(), equationSystem); - createBranchesEquations(networkVector.getBranchVector(), equationSystem); + createMultipleSlackBusesEquations(creationContext); + createBranchesEquations(creationContext); for (LfHvdc hvdc : network.getHvdcs()) { createHvdcAcEmulationEquations(hvdc, equationSystem); } - createVoltageControlEquations(networkVector.getBranchVector(), equationSystem); + createVoltageControlEquations(creationContext); EquationSystemPostProcessor.findAll().forEach(pp -> pp.onCreate(equationSystem)); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java index 056f36da8a..e44cc8894a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java @@ -115,7 +115,7 @@ private void recreateDistributionEquations(LfZeroImpedanceNetwork network) { for (LfBus bus : network.getGraph().vertexSet()) { bus.getGeneratorVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateReactivePowerDistributionEquations(networkVector.getBranchVector(), voltageControl, equationSystem, parameters)); + .ifPresent(voltageControl -> AcEquationSystemCreator.recreateReactivePowerDistributionEquations(voltageControl, new AcEquationSystemCreationContext(equationSystem, networkVector), parameters)); bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, equationSystem)); From cf5205be0408c8635aa5533b325868c579e320c7 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Fri, 28 Apr 2023 22:02:22 +0200 Subject: [PATCH 12/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AbstractBranchAcFlowEquationTerm.java | 25 +++++++--- ...bstractClosedBranchAcFlowEquationTerm.java | 24 ++++------ ...ractOpenSide1BranchAcFlowEquationTerm.java | 12 ++--- ...ractOpenSide2BranchAcFlowEquationTerm.java | 12 ++--- .../ac/equations/AcEquationSystemCreator.java | 48 +++++++++---------- ...osedBranchSide1ActiveFlowEquationTerm.java | 6 +-- ...anchSide1CurrentMagnitudeEquationTerm.java | 6 +-- ...edBranchSide1ReactiveFlowEquationTerm.java | 7 +-- ...osedBranchSide2ActiveFlowEquationTerm.java | 6 +-- ...anchSide2CurrentMagnitudeEquationTerm.java | 6 +-- ...edBranchSide2ReactiveFlowEquationTerm.java | 6 +-- ...OpenBranchSide1ActiveFlowEquationTerm.java | 8 ++-- ...anchSide1CurrentMagnitudeEquationTerm.java | 10 ++-- ...enBranchSide1ReactiveFlowEquationTerm.java | 8 ++-- ...OpenBranchSide2ActiveFlowEquationTerm.java | 8 ++-- ...anchSide2CurrentMagnitudeEquationTerm.java | 14 +++--- ...enBranchSide2ReactiveFlowEquationTerm.java | 8 ++-- .../powsybl/openloadflow/EquationsTest.java | 36 +++++++------- .../network/impl/LfSwitchTest.java | 8 ++-- 19 files changed, 118 insertions(+), 140 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java index 48028d76d2..4a983ac666 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java @@ -1,27 +1,38 @@ /** - * Copyright (c) 2019, RTE (http://www.rte-france.com) + * Copyright (c) 2021, 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.ac.equations; -import com.powsybl.openloadflow.equations.AbstractElementEquationTerm; -import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.equations.AbstractNamedEquationTerm; +import com.powsybl.openloadflow.network.ElementType; import java.util.Objects; /** * @author Geoffroy Jamgotchian */ -abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationTerm { +public abstract class AbstractBranchAcFlowEquationTerm extends AbstractNamedEquationTerm { protected final AcBranchVector branchVector; + protected final int num; - protected AbstractBranchAcFlowEquationTerm(LfBranch branch, AcBranchVector branchVector) { - super(branch); + protected AbstractBranchAcFlowEquationTerm(AcBranchVector branchVector, int num) { + super(!Objects.requireNonNull(branchVector).disabled[num]); this.branchVector = Objects.requireNonNull(branchVector); - this.num = branch.getNum(); + this.num = num; + } + + @Override + public ElementType getElementType() { + return ElementType.BRANCH; + } + + @Override + public int getElementNum() { + return num; } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java index d43f7a11c4..d747f4c3ce 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java @@ -9,8 +9,6 @@ import com.powsybl.math.matrix.DenseMatrix; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.ArrayList; import java.util.List; @@ -37,18 +35,16 @@ public abstract class AbstractClosedBranchAcFlowEquationTerm extends AbstractBra protected final List> variables = new ArrayList<>(); - protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + protected AbstractClosedBranchAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, branchVector); - Objects.requireNonNull(bus1); - Objects.requireNonNull(bus2); + super(branchVector, branchNum); Objects.requireNonNull(variableSet); - v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); - v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); - ph1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_PHI); - ph2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_PHI); - a1Var = deriveA1 ? variableSet.getVariable(branch.getNum(), AcVariableType.BRANCH_ALPHA1) : null; - r1Var = deriveR1 ? variableSet.getVariable(branch.getNum(), AcVariableType.BRANCH_RHO1) : null; + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + ph1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_PHI); + ph2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_PHI); + a1Var = deriveA1 ? variableSet.getVariable(branchNum, AcVariableType.BRANCH_ALPHA1) : null; + r1Var = deriveR1 ? variableSet.getVariable(branchNum, AcVariableType.BRANCH_RHO1) : null; variables.add(v1Var); variables.add(v2Var); variables.add(ph1Var); @@ -82,11 +78,11 @@ protected double ph2() { } protected double r1() { - return r1Var != null ? sv.get(r1Var.getRow()) : element.getPiModel().getR1(); + return r1Var != null ? sv.get(r1Var.getRow()) : branchVector.r1[num]; } protected double a1() { - return a1Var != null ? sv.get(a1Var.getRow()) : element.getPiModel().getA1(); + return a1Var != null ? sv.get(a1Var.getRow()) : branchVector.a1[num]; } public static double theta1(double ksi, double ph1, double a1, double ph2) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java index 9b7196c212..5771ab3227 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.List; @@ -20,13 +18,13 @@ abstract class AbstractOpenSide1BranchAcFlowEquationTerm extends AbstractBranchA protected final List> variables; - protected AbstractOpenSide1BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType, - LfBus bus, AcBranchVector branchVector, + protected AbstractOpenSide1BranchAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, + AcVariableType variableType, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, branchVector); - variables = List.of(variableSet.getVariable(bus.getNum(), variableType)); + super(branchVector, branchNum); + variables = List.of(variableSet.getVariable(bus2Num, variableType)); if (deriveA1 || deriveR1) { - throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branch.getId()); + throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java index 1ca10a9a15..1ab7fd4f58 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.List; @@ -20,13 +18,13 @@ abstract class AbstractOpenSide2BranchAcFlowEquationTerm extends AbstractBranchA protected final List> variables; - protected AbstractOpenSide2BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType, - LfBus bus, AcBranchVector branchVector, + protected AbstractOpenSide2BranchAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, + AcVariableType variableType, int bus1Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, branchVector); - variables = List.of(variableSet.getVariable(bus.getNum(), variableType)); + super(branchVector, branchNum); + variables = List.of(variableSet.getVariable(bus1Num, variableType)); if (deriveA1 || deriveR1) { - throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branch.getId()); + throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 8f8b6759de..d503d7b50d 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -116,8 +116,8 @@ private static void createReactivePowerControlBranchEquation(LfBranch branch, Lf if (bus1 != null && bus2 != null) { branch.getReactivePowerControl().ifPresent(rpc -> { EquationTerm q = rpc.getControlledSide() == ControlledSide.ONE - ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + ? new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1) + : new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_Q) .addTerm(q); @@ -309,12 +309,12 @@ private static List> createReactive boolean deriveR1 = isDeriveR1(branch); if (branch.getBus1() == controllerBus) { LfBus otherSideBus = branch.getBus2(); - q = otherSideBus != null ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, otherSideBus, branchVector, variableSet, deriveA1, deriveR1) - : new OpenBranchSide2ReactiveFlowEquationTerm(branch, controllerBus, branchVector, variableSet, deriveA1, deriveR1); + q = otherSideBus != null ? new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), controllerBus.getNum(), otherSideBus.getNum(), variableSet, deriveA1, deriveR1) + : new OpenBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), controllerBus.getNum(), variableSet, deriveA1, deriveR1); } else { LfBus otherSideBus = branch.getBus1(); - q = otherSideBus != null ? new ClosedBranchSide2ReactiveFlowEquationTerm(branch, otherSideBus, controllerBus, branchVector, variableSet, deriveA1, deriveR1) - : new OpenBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, branchVector, variableSet, deriveA1, deriveR1); + q = otherSideBus != null ? new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), otherSideBus.getNum(), controllerBus.getNum(), variableSet, deriveA1, deriveR1) + : new OpenBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), controllerBus.getNum(), variableSet, deriveA1, deriveR1); } } terms.add(q); @@ -457,8 +457,8 @@ private static void createTransformerPhaseControlEquations(LfBranch branch, LfBu } EquationTerm p = phaseControl.getControlledSide() == ControlledSide.ONE - ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + ? new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1) + : new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_P) .addTerm(p) .setActive(false); // by default BRANCH_TARGET_ALPHA1 is active and BRANCH_TARGET_P inactive @@ -618,20 +618,20 @@ private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcEqu boolean deriveA1 = isDeriveA1(branch, creationParameters); boolean deriveR1 = isDeriveR1(branch); if (bus1 != null && bus2 != null) { - p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); } else if (bus1 != null) { - p1 = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - q1 = new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - i1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = new OpenBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + q1 = new OpenBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + i1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); } else if (bus2 != null) { - p2 = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - q2 = new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, branchVector, equationSystem.getVariableSet(), deriveA1, deriveR1); + p2 = new OpenBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + q2 = new OpenBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + i2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); } if (p1 != null) { @@ -730,13 +730,13 @@ private List> createActiveInjection boolean deriveR1 = isDeriveR1(branch); if (branch.getBus1() == bus) { LfBus otherSideBus = branch.getBus2(); - var p = otherSideBus != null ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus, otherSideBus, branchVector, variableSet, deriveA1, deriveR1) - : new OpenBranchSide2ActiveFlowEquationTerm(branch, bus, branchVector, variableSet, deriveA1, deriveR1); + var p = otherSideBus != null ? new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus.getNum(), otherSideBus.getNum(), variableSet, deriveA1, deriveR1) + : new OpenBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus.getNum(), variableSet, deriveA1, deriveR1); terms.add(p); } else { LfBus otherSideBus = branch.getBus1(); - var p = otherSideBus != null ? new ClosedBranchSide2ActiveFlowEquationTerm(branch, otherSideBus, bus, branchVector, variableSet, deriveA1, deriveR1) - : new OpenBranchSide1ActiveFlowEquationTerm(branch, bus, branchVector, variableSet, deriveA1, deriveR1); + var p = otherSideBus != null ? new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), otherSideBus.getNum(), bus.getNum(), variableSet, deriveA1, deriveR1) + : new OpenBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus.getNum(), variableSet, deriveA1, deriveR1); terms.add(p); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index 425db80464..94725d91f7 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -22,9 +20,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1ActiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + public ClosedBranchSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index 0c14973a8c..efc6b6f6cf 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -23,9 +21,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1CurrentMagnitudeEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + public ClosedBranchSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index d20ae3f0fd..5a827b7576 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -22,15 +20,14 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1ReactiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + public ClosedBranchSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; - double g1 = branchVector.g1[num]; double b1 = branchVector.b1[num]; double v1 = v1(); double r1 = r1(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index e92026f6c7..ac1ccb8fb2 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -22,9 +20,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2ActiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + public ClosedBranchSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java index 641d401466..99a2a76a18 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -23,9 +21,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2CurrentMagnitudeEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + public ClosedBranchSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index d7eef6466b..469d81f694 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -22,9 +20,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2ReactiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, + public ClosedBranchSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, bus1, bus2, branchVector, variableSet, deriveA1, deriveR1); + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java index 72b3307145..959b863645 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.Objects; @@ -22,10 +20,10 @@ public class OpenBranchSide1ActiveFlowEquationTerm extends AbstractOpenSide1Bran private final Variable v2Var; - public OpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcBranchVector branchVector, + public OpenBranchSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus2, branchVector, variableSet, deriveA1, deriveR1); - v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); } public static double p2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java index 2fdaee0695..935e414600 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -26,11 +24,11 @@ public class OpenBranchSide1CurrentMagnitudeEquationTerm extends AbstractOpenSid private final Variable ph2Var; - public OpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, AcBranchVector branchVector, + public OpenBranchSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus2, branchVector, variableSet, deriveA1, deriveR1); - v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); - ph2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_PHI); + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + ph2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_PHI); } private double v2() { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java index 47b53617ef..7c0ebedf69 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.Objects; @@ -22,10 +20,10 @@ public class OpenBranchSide1ReactiveFlowEquationTerm extends AbstractOpenSide1Br private final Variable v2Var; - public OpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcBranchVector branchVector, + public OpenBranchSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus2, branchVector, variableSet, deriveA1, deriveR1); - v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); } public static double q2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java index 2ace28ac1e..cd681d37b8 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.Objects; @@ -20,10 +18,10 @@ public class OpenBranchSide2ActiveFlowEquationTerm extends AbstractOpenSide2Bran private final Variable v1Var; - public OpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcBranchVector branchVector, + public OpenBranchSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus1, branchVector, variableSet, deriveA1, deriveR1); - v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); } public static double p1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java index 0faa4a982a..8c85a34a70 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -26,13 +24,13 @@ public class OpenBranchSide2CurrentMagnitudeEquationTerm extends AbstractOpenSid private Variable r1Var; - public OpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, AcBranchVector branchVector, + public OpenBranchSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus1, branchVector, variableSet, deriveA1, deriveR1); - v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); - ph1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_PHI); + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + ph1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_PHI); if (deriveR1) { - r1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BRANCH_RHO1); + r1Var = variableSet.getVariable(bus1Num, AcVariableType.BRANCH_RHO1); } } @@ -45,7 +43,7 @@ private double ph1() { } private double r1() { - return r1Var != null ? sv.get(r1Var.getRow()) : element.getPiModel().getR1(); + return r1Var != null ? sv.get(r1Var.getRow()) : branchVector.r1[num]; } private static double gres(double y, double sinksi, double g1, double g2, double b2, double shunt) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java index 3ed108f6ff..d9b2474c4d 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import java.util.Objects; @@ -20,10 +18,10 @@ public class OpenBranchSide2ReactiveFlowEquationTerm extends AbstractOpenSide2Br private final Variable v1Var; - public OpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcBranchVector branchVector, + public OpenBranchSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branch, AcVariableType.BUS_V, bus1, branchVector, variableSet, deriveA1, deriveR1); - v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); } public static double q1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index a8bc5694a7..c9228a5d42 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -182,55 +182,55 @@ void branchTest() { // closed branch equations AcBranchVector branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {41.78173051479356, 48.66261692116701, 138.21343172859858, 29.31710523088579, -138.21343172859858, 54.62161149356045, 138.21343172859858, Double.NaN, 270.81476537421185}, - eval(new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-3.500079625302254, 122.46444997806617, 31.42440177840898, -128.9449438332101, -31.42440177840898, 137.46086897280827, 31.42440177840898, Double.NaN, 162.40477689607334}, - eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {39.13246485286219, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, - eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-40.6365773800554, -48.52391742324069, -131.8614376204652, -27.319027760225953, 131.8614376204652, -54.4659275092331, -131.8614376204652, Double.NaN, -262.1703103131649}, - eval(new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {16.04980301110306, -123.06939783256767, 51.99045110393844, 152.96594042215764, -51.99045110393844, -138.1398958886022, 51.99045110393844, Double.NaN, -56.2529021950738}, - eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {40.76137216481359, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, - eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); // open branch equations branchVector = createBranchVector(null, bus2, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {0.1717595025847833, Double.NaN, Double.NaN, 0.3204828812456483, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.3752024940555977, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); branchVector = createBranchVector(bus1, null, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {0.15639470221220209, Double.NaN, Double.NaN, 0.2919337476186018, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2ActiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.33122369717493005, Double.NaN, Double.NaN, -0.6182778179094991, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {0.34186721585930596, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus2, branchVector, variableSet, false, false), variables, sv)); + eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); // assert current equation is consistent with active and reactive power ones - var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); + var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); p1Eq.setStateVector(sv); double p1 = p1Eq.eval(); - var q1Eq = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); + var q1Eq = new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); q1Eq.setStateVector(sv); double q1 = q1Eq.eval(); - var i1Eq = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); + var i1Eq = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); i1Eq.setStateVector(sv); double i1 = i1Eq.eval(); assertEquals(i1, Math.hypot(p1, q1) / V_1, 10e-14); - var p2Eq = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); + var p2Eq = new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); p2Eq.setStateVector(sv); double p2 = p2Eq.eval(); - var q2Eq = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); + var q2Eq = new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); q2Eq.setStateVector(sv); double q2 = q2Eq.eval(); - var i2Eq = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, branchVector, variableSet, true, true); + var i2Eq = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); i2Eq.setStateVector(sv); double i2 = i2Eq.eval(); assertEquals(i2, Math.hypot(p2, q2) / V_2, 10e-14); diff --git a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java index e56a5bc911..167f260e79 100644 --- a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java +++ b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java @@ -74,15 +74,15 @@ void setterTest() { AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem, creationParameters); VariableSet variableSet = new VariableSet<>(); - EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); - EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); + EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); + EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); lfSwitch.setP1(p1); assertEquals(Double.NaN, lfSwitch.getP1().eval()); lfSwitch.setP2(p2); assertEquals(Double.NaN, lfSwitch.getP2().eval()); - EquationTerm i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); - EquationTerm i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), networkVector.getBranchVector(), variableSet, false, false); + EquationTerm i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); + EquationTerm i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); lfSwitch.setI1(i1); assertEquals(Double.NaN, lfSwitch.getP1().eval()); lfSwitch.setI2(i2); From 91d7f6d93806311c049ddf8120264640f14eb2fd Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Fri, 28 Apr 2023 22:39:46 +0200 Subject: [PATCH 13/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcBranchVector.java | 3 +++ .../ac/equations/AcBusVector.java | 2 ++ .../ac/equations/AcNetworkVector.java | 21 +++++++++++++------ .../powsybl/openloadflow/EquationsTest.java | 4 ++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java index 16ecdf9cce..cb5afb8795 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java @@ -14,6 +14,9 @@ import java.util.List; /** + * Vectorized view of the branches and variables related to branches. + * Are included all power flows and theirs partial derivatives. + * * @author Geoffroy Jamgotchian */ public class AcBranchVector { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java index 03772c739c..d632554f68 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java @@ -11,6 +11,8 @@ import java.util.List; /** + * Vectorized view of the buses. Only variables related the buses at the moment. + * * @author Geoffroy Jamgotchian */ public class AcBusVector { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 069ca07192..b9abd89e0b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -19,6 +19,8 @@ import java.util.concurrent.TimeUnit; /** + * Vectorized view of the network and variables of the equation system. + * * @author Geoffroy Jamgotchian */ public class AcNetworkVector extends AbstractLfNetworkListener @@ -60,6 +62,9 @@ public void startListening() { equationSystem.getStateVector().addListener(this); } + /** + * Update vectorized view of the variables from the equation system. + */ public void updateVariables() { if (!variablesInvalid) { return; @@ -100,15 +105,16 @@ public void updateVariables() { break; } } - updateBranchVariables(); + + copyVariablesToBranches(); stopwatch.stop(); - LOGGER.info("AC variable vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); + LOGGER.debug("AC variable vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); variablesInvalid = false; } - public void updateBranchVariables() { + public void copyVariablesToBranches() { for (int branchNum = 0; branchNum < branchVector.getSize(); branchNum++) { if (branchVector.bus1Num[branchNum] != -1) { branchVector.v1Row[branchNum] = busVector.vRow[branchVector.bus1Num[branchNum]]; @@ -121,7 +127,10 @@ public void updateBranchVariables() { } } - public void updateNetwork() { + /** + * Update all power flows and their derivatives. + */ + public void updatePowerFlows() { Stopwatch stopwatch = Stopwatch.createStarted(); double[] state = equationSystem.getStateVector().get(); @@ -470,7 +479,7 @@ public void updateNetwork() { } stopwatch.stop(); - LOGGER.info("AC network vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); + LOGGER.debug("AC network vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); } @Override @@ -505,6 +514,6 @@ public void onEquationTermChange(EquationTerm te @Override public void onStateUpdate() { updateVariables(); - updateNetwork(); + updatePowerFlows(); } } diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index c9228a5d42..6d517b9f17 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -150,8 +150,8 @@ private AcBranchVector createBranchVector(LfBus bus1, LfBus bus2, boolean derive busVector.phRow[1] = ph2Var.getRow(); branchVector.a1Row[0] = a1Var.getRow(); branchVector.r1Row[0] = r1Var.getRow(); - networkVector.updateBranchVariables(); - networkVector.updateNetwork(); + networkVector.copyVariablesToBranches(); + networkVector.updatePowerFlows(); return networkVector.getBranchVector(); } From ce82e2395431240770d2790c508e26bc5b104ff5 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 31 May 2023 21:18:26 +0200 Subject: [PATCH 14/32] Fix merge Signed-off-by: Geoffroy Jamgotchian --- ...bstractClosedBranchAcFlowEquationTerm.java | 2 - .../ac/equations/AcBranchVector.java | 7 ++++ .../ac/equations/AcEquationSystemCreator.java | 4 +- .../ClosedBranchI1xFlowEquationTerm.java | 19 +++++----- .../ClosedBranchI1yFlowEquationTerm.java | 19 +++++----- .../ClosedBranchI2xFlowEquationTerm.java | 19 +++++----- .../ClosedBranchI2yFlowEquationTerm.java | 19 +++++----- ...osedBranchSide1ActiveFlowEquationTerm.java | 2 - ...anchSide1CurrentMagnitudeEquationTerm.java | 2 - ...edBranchSide1ReactiveFlowEquationTerm.java | 2 - ...osedBranchSide2ActiveFlowEquationTerm.java | 2 - ...anchSide2CurrentMagnitudeEquationTerm.java | 2 - ...edBranchSide2ReactiveFlowEquationTerm.java | 2 - .../AsymmetricalAcEquationSystemCreator.java | 37 ++++++++++--------- 14 files changed, 65 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java index 31da249ffa..721c24232c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java @@ -9,8 +9,6 @@ import com.powsybl.math.matrix.DenseMatrix; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import java.util.ArrayList; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java index cb5afb8795..15b116d802 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java @@ -25,6 +25,8 @@ public class AcBranchVector { final int[] bus2Num; final double[] y; + final double[] g12; + final double[] b12; final double[] ksi; final double[] cosKsi; final double[] sinKsi; @@ -88,6 +90,8 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter bus1Num = new int[size]; bus2Num = new int[size]; y = new double[size]; + g12 = new double[size]; + b12 = new double[size]; ksi = new double[size]; cosKsi = new double[size]; sinKsi = new double[size]; @@ -155,6 +159,9 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter // throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); // } y[i] = piModel.getY(); + // y12 = g12+j.b12 = 1/(r+j.x) + g12[i] = piModel.getR() * y[i] * y[i]; + b12[i] = -piModel.getX() * y[i] * y[i]; ksi[i] = piModel.getKsi(); cosKsi[i] = FastMath.cos(ksi[i]); sinKsi[i] = FastMath.sin(ksi[i]); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 42588f0b9c..6462bd5bb2 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -637,9 +637,9 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE createBranchEquations(branch, bus1, bus2, equationSystem, p1, q1, p2, q2, i1, i2); - createReactivePowerControlBranchEquation(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createReactivePowerControlBranchEquation(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); - createTransformerPhaseControlEquations(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createTransformerPhaseControlEquations(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); } protected static void createBranchEquations(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java index 30101dbea7..b0518dd199 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java @@ -10,8 +10,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -23,9 +21,10 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI1xFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI1xFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI1xFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -61,20 +60,20 @@ private static double di1xdph2(double v2, double ph2, double g12, double b12) { @Override public double eval() { - return i1x(g1, b1, v1(), ph1(), v2(), ph2(), g12, b12); + return i1x(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di1xdv1(g1, b1, ph1(), g12, b12); + return di1xdv1(branchVector.g1[num], branchVector.b1[num], ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(v2Var)) { - return di1xdv2(ph2(), g12, b12); + return di1xdv2(ph2(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph1Var)) { - return di1xdph1(g1, b1, v1(), ph1(), g12, b12); + return di1xdph1(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph2Var)) { - return di1xdph2(v2(), ph2(), g12, b12); + return di1xdph2(v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java index bd9d234d2a..605697f86b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java @@ -10,8 +10,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -23,9 +21,10 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI1yFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI1yFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI1yFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -61,20 +60,20 @@ private static double di1ydph2(double v2, double ph2, double g12, double b12) { @Override public double eval() { - return i1y(g1, b1, v1(), ph1(), v2(), ph2(), g12, b12); + return i1y(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di1ydv1(g1, b1, ph1(), g12, b12); + return di1ydv1(branchVector.g1[num], branchVector.b1[num], ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(v2Var)) { - return di1ydv2(ph2(), g12, b12); + return di1ydv2(ph2(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph1Var)) { - return di1ydph1(g1, b1, v1(), ph1(), g12, b12); + return di1ydph1(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph2Var)) { - return di1ydph2(v2(), ph2(), g12, b12); + return di1ydph2(v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java index e9aebe72d5..3f4dd69019 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java @@ -10,8 +10,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -23,9 +21,10 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI2xFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI2xFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI2xFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -71,20 +70,20 @@ private static double di2xdph2(double g2, double b2, double v2, double ph2, doub @Override public double eval() { - return i2x(g2, b2, v1(), ph1(), v2(), ph2(), g12, b12); + return i2x(branchVector.g2[num], branchVector.b2[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di2xdv1(ph1(), g12, b12); + return di2xdv1(ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(v2Var)) { - return di2xdv2(g2, b2, ph2(), g12, b12); + return di2xdv2(branchVector.g2[num], branchVector.b2[num], ph2(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph1Var)) { - return di2xdph1(v1(), ph1(), g12, b12); + return di2xdph1(v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph2Var)) { - return di2xdph2(g2, b2, v2(), ph2(), g12, b12); + return di2xdph2(branchVector.g2[num], branchVector.b2[num], v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java index e3657cceb1..81d4dca278 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java @@ -10,8 +10,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -23,9 +21,10 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI2yFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI2yFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, - boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { - super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI2yFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -71,20 +70,20 @@ private static double di2ydph2(double g2, double b2, double v2, double ph2, doub @Override public double eval() { - return i2y(g2, b2, v1(), ph1(), v2(), ph2(), g12, b12); + return i2y(branchVector.g2[num], branchVector.b2[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di2ydv1(ph1(), g12, b12); + return di2ydv1(ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(v2Var)) { - return di2ydv2(g2, b2, ph2(), g12, b12); + return di2ydv2(branchVector.g2[num], branchVector.b2[num], ph2(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph1Var)) { - return di2ydph1(v1(), ph1(), g12, b12); + return di2ydph1(v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); } else if (variable.equals(ph2Var)) { - return di2ydph2(g2, b2, v2(), ph2(), g12, b12); + return di2ydph2(branchVector.g2[num], branchVector.b2[num], v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index 6e98a6e40c..0f8246c14a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index b673a2685c..84930e94fe 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index b520f27559..c9dd3ae33c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index 59d8a56dc5..de335eee73 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java index 2f84fac2dc..9cb4f71b51 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index 8c415252dd..4d5f6136fe 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBranch; -import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index c623b00522..3e13c096ea 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -97,7 +97,10 @@ protected void createBusEquation(LfBus bus, EquationSystem equationSystem) { + protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); + var branchVector = creationContext.getNetworkVector().getBranchVector(); + // positive sequence EquationTerm p1 = null; EquationTerm q1 = null; @@ -125,24 +128,24 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, Equ if (!branch.isAsymmetric()) { // no asymmetry is detected with this line, we handle the equations as decoupled // positive - p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); // zero - ixz1 = new ClosedBranchI1xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); - iyz1 = new ClosedBranchI1yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); - ixz2 = new ClosedBranchI2xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); - iyz2 = new ClosedBranchI2yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + ixz1 = new ClosedBranchI1xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + iyz1 = new ClosedBranchI1yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + ixz2 = new ClosedBranchI2xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + iyz2 = new ClosedBranchI2yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); // negative - ixn1 = new ClosedBranchI1xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); - iyn1 = new ClosedBranchI1yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); - ixn2 = new ClosedBranchI2xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); - iyn2 = new ClosedBranchI2yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + ixn1 = new ClosedBranchI1xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + iyn1 = new ClosedBranchI1yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + ixn2 = new ClosedBranchI2xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + iyn2 = new ClosedBranchI2yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); } else { // assymmetry is detected with this line, we handle the equations as coupled between the different sequences // positive @@ -216,8 +219,8 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, Equ .addTerm(iyn2); } - createReactivePowerControlBranchEquation(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createReactivePowerControlBranchEquation(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); - createTransformerPhaseControlEquations(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createTransformerPhaseControlEquations(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); } } From cc3e013b0ebd7730ba4b4828e00d890e01699f8e Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 27 Jun 2023 20:57:25 +0200 Subject: [PATCH 15/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AbstractShuntCompensatorEquationTerm.java | 30 ++++++--- .../ac/equations/AcEquationSystemCreator.java | 33 ++++----- .../ac/equations/AcNetworkVector.java | 23 +++++++ .../ac/equations/AcShuntVector.java | 67 +++++++++++++++++++ ...huntCompensatorActiveFlowEquationTerm.java | 18 ++--- ...ntCompensatorReactiveFlowEquationTerm.java | 22 +++--- .../AsymmetricalAcEquationSystemCreator.java | 6 +- .../openloadflow/network/AbstractLfShunt.java | 12 +++- .../powsybl/openloadflow/network/LfShunt.java | 2 + .../network/LfStandbyAutomatonShunt.java | 2 +- .../network/impl/LfShuntImpl.java | 2 +- .../powsybl/openloadflow/EquationsTest.java | 8 +-- 12 files changed, 169 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java index a0574a54a0..fb92c2b236 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java @@ -6,26 +6,40 @@ */ package com.powsybl.openloadflow.ac.equations; -import com.powsybl.openloadflow.equations.AbstractElementEquationTerm; +import com.powsybl.openloadflow.equations.AbstractNamedEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBus; -import com.powsybl.openloadflow.network.LfShunt; +import com.powsybl.openloadflow.network.ElementType; import java.util.Objects; /** * @author Geoffroy Jamgotchian */ -public abstract class AbstractShuntCompensatorEquationTerm extends AbstractElementEquationTerm { +public abstract class AbstractShuntCompensatorEquationTerm extends AbstractNamedEquationTerm { + + protected final AcShuntVector shuntVector; + + protected final int num; protected final Variable vVar; - protected AbstractShuntCompensatorEquationTerm(LfShunt shunt, LfBus bus, VariableSet variableSet) { - super(shunt); - Objects.requireNonNull(bus); + protected AbstractShuntCompensatorEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet) { + super(!Objects.requireNonNull(shuntVector).disabled[num]); + this.shuntVector = Objects.requireNonNull(shuntVector); + this.num = num; Objects.requireNonNull(variableSet); - vVar = variableSet.getVariable(bus.getNum(), AcVariableType.BUS_V); + vVar = variableSet.getVariable(busNum, AcVariableType.BUS_V); + } + + @Override + public ElementType getElementType() { + return ElementType.SHUNT_COMPENSATOR; + } + + @Override + public int getElementNum() { + return num; } protected double v() { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index c15511f2ff..9ed510c6b9 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -36,8 +36,8 @@ public AcEquationSystemCreator(LfNetwork network, AcEquationSystemCreationParame this.creationParameters = Objects.requireNonNull(creationParameters); } - protected void createBusEquation(LfBus bus, - EquationSystem equationSystem) { + protected void createBusEquation(LfBus bus, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); var p = equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_P); bus.setP(p); var q = equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_Q); @@ -60,7 +60,7 @@ protected void createBusEquation(LfBus bus, .setActive(false); bus.setCalculatedV(vTerm); - createShuntEquations(bus, equationSystem); + createShuntEquations(bus, creationContext); } private void createVoltageControlEquations(AcEquationSystemCreationContext creationContext) { @@ -71,9 +71,9 @@ private void createVoltageControlEquations(AcEquationSystemCreationContext creat } } - private void createBusesEquations(EquationSystem equationSystem) { + private void createBusesEquations(AcEquationSystemCreationContext creationContext) { for (LfBus bus : network.getBuses()) { - createBusEquation(bus, equationSystem); + createBusEquation(bus, creationContext); } } @@ -139,19 +139,21 @@ public static void updateReactivePowerControlBranchEquations(ReactivePowerContro .setActive(false); } - private static void createShuntEquation(LfShunt shunt, LfBus bus, EquationSystem equationSystem, boolean deriveB) { - ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shunt, bus, equationSystem.getVariableSet(), deriveB); + private static void createShuntEquation(LfShunt shunt, LfBus bus, boolean deriveB, AcEquationSystemCreationContext creationContext) { + AcShuntVector shuntVector = creationContext.getNetworkVector().getShuntVector(); + var equationSystem = creationContext.getEquationSystem(); + ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shuntVector, shunt.getNum(), bus.getNum(), equationSystem.getVariableSet(), deriveB); equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_Q).addTerm(q); shunt.setQ(q); - ShuntCompensatorActiveFlowEquationTerm p = new ShuntCompensatorActiveFlowEquationTerm(shunt, bus, equationSystem.getVariableSet()); + ShuntCompensatorActiveFlowEquationTerm p = new ShuntCompensatorActiveFlowEquationTerm(shuntVector, shunt.getNum(), bus.getNum(), equationSystem.getVariableSet()); equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_P).addTerm(p); shunt.setP(p); } - private static void createShuntEquations(LfBus bus, EquationSystem equationSystem) { - bus.getShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, equationSystem, false)); - bus.getControllerShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, equationSystem, shunt.hasVoltageControlCapability())); - bus.getSvcShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, equationSystem, false)); + private static void createShuntEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { + bus.getShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, false, creationContext)); + bus.getControllerShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, shunt.hasVoltageControlCapability(), creationContext)); + bus.getSvcShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, false, creationContext)); } private static void createReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, @@ -320,12 +322,13 @@ private static List> createReactive } terms.add(q); } + AcShuntVector shuntVector = creationContext.getNetworkVector().getShuntVector(); controllerBus.getShunt().ifPresent(shunt -> { - ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shunt, controllerBus, variableSet, false); + ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shuntVector, shunt.getNum(), controllerBus.getNum(), variableSet, false); terms.add(q); }); controllerBus.getControllerShunt().ifPresent(shunt -> { - ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shunt, controllerBus, variableSet, false); + ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shuntVector, shunt.getNum(), controllerBus.getNum(), variableSet, false); terms.add(q); }); return terms; @@ -779,7 +782,7 @@ public EquationSystem create() { AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); AcEquationSystemCreationContext creationContext = new AcEquationSystemCreationContext(equationSystem, networkVector); - createBusesEquations(equationSystem); + createBusesEquations(creationContext); createMultipleSlackBusesEquations(creationContext); createBranchesEquations(creationContext); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index b9abd89e0b..7168370acb 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -32,6 +32,7 @@ public class AcNetworkVector extends AbstractLfNetworkListener private final EquationSystem equationSystem; private final AcBusVector busVector; private final AcBranchVector branchVector; + private final AcShuntVector shuntVector; private boolean variablesInvalid = true; public AcNetworkVector(LfNetwork network, EquationSystem equationSystem, @@ -40,6 +41,7 @@ public AcNetworkVector(LfNetwork network, EquationSystem v : equationSystem.getIndex().getSortedVariablesToFind()) { int num = v.getElementNum(); @@ -101,6 +108,10 @@ public void updateVariables() { branchVector.r1Row[num] = branchVector.deriveR1[num] ? row : -1; break; + case SHUNT_B: + shuntVector.bRow[num] = shuntVector.deriveB[num] ? row : -1; + break; + default: break; } @@ -478,6 +489,16 @@ public void updatePowerFlows() { } } + for (int shuntNum = 0; shuntNum < shuntVector.getSize(); shuntNum++) { + double v = state[busVector.vRow[shuntVector.busNum[shuntNum]]]; + double b = shuntVector.bRow[shuntNum] != -1 ? state[shuntVector.bRow[shuntNum]] : shuntVector.b[shuntNum]; + shuntVector.p[shuntNum] = ShuntCompensatorActiveFlowEquationTerm.p(v, shuntVector.g[shuntNum]); + shuntVector.dpdv[shuntNum] = ShuntCompensatorActiveFlowEquationTerm.dpdv(v, shuntVector.g[shuntNum]); + shuntVector.q[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.q(v, b); + shuntVector.dqdv[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.dqdv(v, b); + shuntVector.dqdb[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.dqdb(v); + } + stopwatch.stop(); LOGGER.debug("AC network vector update in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); } @@ -486,6 +507,8 @@ public void updatePowerFlows() { public void onDisableChange(LfElement element, boolean disabled) { if (element.getType() == ElementType.BRANCH) { branchVector.disabled[element.getNum()] = disabled; + } else if (element.getType() == ElementType.SHUNT_COMPENSATOR) { + shuntVector.disabled[element.getNum()] = disabled; } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java new file mode 100644 index 0000000000..8895355dcc --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java @@ -0,0 +1,67 @@ +/** + * 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.ac.equations; + +import com.powsybl.openloadflow.network.LfBus; +import com.powsybl.openloadflow.network.LfShunt; + +import java.util.List; + +/** + * @author Geoffroy Jamgotchian + */ +public class AcShuntVector { + + final int[] busNum; + + final boolean[] disabled; + + final double[] g; + final double[] b; + + final boolean[] deriveB; + + public final int[] bRow; + + final double[] p; + final double[] q; + + final double[] dpdv; + final double[] dqdv; + final double[] dqdb; + + public AcShuntVector(List shunts) { + int size = shunts.size(); + busNum = new int[size]; + disabled = new boolean[size]; + g = new double[size]; + b = new double[size]; + deriveB = new boolean[size]; + + bRow = new int[size]; + + p = new double[size]; + q = new double[size]; + dpdv = new double[size]; + dqdv = new double[size]; + dqdb = new double[size]; + + for (int i = 0; i < size; i++) { + LfShunt shunt = shunts.get(i); + LfBus bus = shunt.getBus(); + busNum[i] = bus != null ? bus.getNum() : -1; + disabled[i] = shunt.isDisabled(); + g[i] = shunt.getG(); + b[i] = shunt.getB(); + deriveB[i] = shunt.hasVoltageControlCapability(); + } + } + + public int getSize() { + return busNum.length; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java index ead5327b41..a881fd350b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java @@ -8,8 +8,6 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBus; -import com.powsybl.openloadflow.network.LfShunt; import java.util.List; import java.util.Objects; @@ -21,8 +19,8 @@ public class ShuntCompensatorActiveFlowEquationTerm extends AbstractShuntCompens private final List> variables; - public ShuntCompensatorActiveFlowEquationTerm(LfShunt shunt, LfBus bus, VariableSet variableSet) { - super(shunt, bus, variableSet); + public ShuntCompensatorActiveFlowEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet) { + super(shuntVector, num, busNum, variableSet); variables = List.of(vVar); } @@ -31,28 +29,24 @@ public List> getVariables() { return variables; } - private double g() { - return element.getG(); - } - - private static double p(double v, double g) { + public static double p(double v, double g) { return g * v * v; } - private static double dpdv(double v, double g) { + public static double dpdv(double v, double g) { return 2 * g * v; } @Override public double eval() { - return p(v(), g()); + return shuntVector.p[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(vVar)) { - return dpdv(v(), g()); + return shuntVector.dpdv[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java index 91a6e9505d..b9d3453e9a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java @@ -9,8 +9,6 @@ import com.powsybl.math.matrix.DenseMatrix; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.LfBus; -import com.powsybl.openloadflow.network.LfShunt; import java.util.List; import java.util.Objects; @@ -24,10 +22,10 @@ public class ShuntCompensatorReactiveFlowEquationTerm extends AbstractShuntCompe private final List> variables; - public ShuntCompensatorReactiveFlowEquationTerm(LfShunt shunt, LfBus bus, VariableSet variableSet, boolean deriveB) { - super(shunt, bus, variableSet); + public ShuntCompensatorReactiveFlowEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet, boolean deriveB) { + super(shuntVector, num, busNum, variableSet); if (deriveB) { - bVar = variableSet.getVariable(shunt.getNum(), AcVariableType.SHUNT_B); + bVar = variableSet.getVariable(num, AcVariableType.SHUNT_B); variables = List.of(vVar, bVar); } else { variables = List.of(vVar); @@ -40,33 +38,33 @@ public List> getVariables() { } private double b() { - return bVar != null ? sv.get(bVar.getRow()) : element.getB(); + return bVar != null ? sv.get(bVar.getRow()) : shuntVector.b[num]; } - private static double q(double v, double b) { + public static double q(double v, double b) { return -b * v * v; } - private static double dqdv(double v, double b) { + public static double dqdv(double v, double b) { return -2 * b * v; } - private static double dqdb(double v) { + public static double dqdb(double v) { return -v * v; } @Override public double eval() { - return q(v(), b()); + return shuntVector.q[num]; } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(vVar)) { - return dqdv(v(), b()); + return shuntVector.dqdv[num]; } else if (variable.equals(bVar)) { - return dqdb(v()); + return shuntVector.dqdb[num]; } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index 3e13c096ea..7d9b884f95 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -28,8 +28,10 @@ public AsymmetricalAcEquationSystemCreator(LfNetwork network, AcEquationSystemCr } @Override - protected void createBusEquation(LfBus bus, EquationSystem equationSystem) { - super.createBusEquation(bus, equationSystem); + protected void createBusEquation(LfBus bus, AcEquationSystemCreationContext creationContext) { + super.createBusEquation(bus, creationContext); + + var equationSystem = creationContext.getEquationSystem(); // addition of asymmetric equations, supposing that existing v, theta, p and q are linked to the positive sequence LfAsymBus asymBus = bus.getAsym(); diff --git a/src/main/java/com/powsybl/openloadflow/network/AbstractLfShunt.java b/src/main/java/com/powsybl/openloadflow/network/AbstractLfShunt.java index 7aefc48784..8617a95f55 100644 --- a/src/main/java/com/powsybl/openloadflow/network/AbstractLfShunt.java +++ b/src/main/java/com/powsybl/openloadflow/network/AbstractLfShunt.java @@ -9,17 +9,27 @@ import com.powsybl.openloadflow.util.Evaluable; import com.powsybl.openloadflow.util.EvaluableConstants; +import java.util.Objects; + /** * @author Geoffroy Jamgotchian */ public abstract class AbstractLfShunt extends AbstractElement implements LfShunt { + private final LfBus bus; + private Evaluable q = EvaluableConstants.NAN; private Evaluable p = EvaluableConstants.NAN; - protected AbstractLfShunt(LfNetwork network) { + protected AbstractLfShunt(LfBus bus, LfNetwork network) { super(network); + this.bus = Objects.requireNonNull(bus); + } + + @Override + public LfBus getBus() { + return bus; } @Override diff --git a/src/main/java/com/powsybl/openloadflow/network/LfShunt.java b/src/main/java/com/powsybl/openloadflow/network/LfShunt.java index 98ef7edbab..32d8b50c13 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfShunt.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfShunt.java @@ -115,6 +115,8 @@ public Optional updateSectionB(double deltaB, int maxSectionShift, Al } } + LfBus getBus(); + double getB(); default double getBMagnitude() { diff --git a/src/main/java/com/powsybl/openloadflow/network/LfStandbyAutomatonShunt.java b/src/main/java/com/powsybl/openloadflow/network/LfStandbyAutomatonShunt.java index 343fcfa394..ccdd4361b8 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfStandbyAutomatonShunt.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfStandbyAutomatonShunt.java @@ -24,7 +24,7 @@ public final class LfStandbyAutomatonShunt extends AbstractLfShunt { private double b; private LfStandbyAutomatonShunt(LfStaticVarCompensator svc) { - super(svc.getBus().getNetwork()); + super(svc.getBus(), svc.getBus().getNetwork()); this.svc = svc; double zb = PerUnit.zb(svc.getBus().getNominalV()); b = svc.getB0() * zb; 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 c4b9097c15..a13bdea1e0 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java @@ -70,7 +70,7 @@ public LfShuntImpl(List shuntCompensators, LfNetwork network, // 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); + super(bus, network); shuntCompensatorsRefs = Objects.requireNonNull(shuntCompensators).stream() .map(sc -> Ref.create(sc, parameters.isCacheEnabled())) .collect(Collectors.toList()); diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 6d517b9f17..64fa9840ca 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -281,10 +281,10 @@ void shuntTest() { var sv = new StateVector(new double[] {V_1, B, 0}); - assertArrayEquals(new double[] {4.275919696380507E-5, 7.98163392892194E-5, Double.NaN, Double.NaN, Double.NaN}, - eval(new ShuntCompensatorActiveFlowEquationTerm(shunt, bus, variableSet), variables, sv)); - assertArrayEquals(new double[] {-0.3155098135679268, -0.588945539602459, -1.1479830120627779, Double.NaN, -1.7369285516652369}, - eval(new ShuntCompensatorReactiveFlowEquationTerm(shunt, bus, variableSet, true), variables, sv)); +// assertArrayEquals(new double[] {4.275919696380507E-5, 7.98163392892194E-5, Double.NaN, Double.NaN, Double.NaN}, +// eval(new ShuntCompensatorActiveFlowEquationTerm(shunt, bus, variableSet), variables, sv)); +// assertArrayEquals(new double[] {-0.3155098135679268, -0.588945539602459, -1.1479830120627779, Double.NaN, -1.7369285516652369}, +// eval(new ShuntCompensatorReactiveFlowEquationTerm(shunt, bus, variableSet, true), variables, sv)); } @Test From d70277167a173270ffb4353048585b075646be24 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 27 Jun 2023 21:03:07 +0200 Subject: [PATCH 16/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcNetworkVector.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index 7168370acb..fe8732304f 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -490,13 +490,17 @@ public void updatePowerFlows() { } for (int shuntNum = 0; shuntNum < shuntVector.getSize(); shuntNum++) { - double v = state[busVector.vRow[shuntVector.busNum[shuntNum]]]; - double b = shuntVector.bRow[shuntNum] != -1 ? state[shuntVector.bRow[shuntNum]] : shuntVector.b[shuntNum]; - shuntVector.p[shuntNum] = ShuntCompensatorActiveFlowEquationTerm.p(v, shuntVector.g[shuntNum]); - shuntVector.dpdv[shuntNum] = ShuntCompensatorActiveFlowEquationTerm.dpdv(v, shuntVector.g[shuntNum]); - shuntVector.q[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.q(v, b); - shuntVector.dqdv[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.dqdv(v, b); - shuntVector.dqdb[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.dqdb(v); + if (!shuntVector.disabled[shuntNum]) { + if (shuntVector.busNum[shuntNum] != -1) { + double v = state[busVector.vRow[shuntVector.busNum[shuntNum]]]; + double b = shuntVector.bRow[shuntNum] != -1 ? state[shuntVector.bRow[shuntNum]] : shuntVector.b[shuntNum]; + shuntVector.p[shuntNum] = ShuntCompensatorActiveFlowEquationTerm.p(v, shuntVector.g[shuntNum]); + shuntVector.dpdv[shuntNum] = ShuntCompensatorActiveFlowEquationTerm.dpdv(v, shuntVector.g[shuntNum]); + shuntVector.q[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.q(v, b); + shuntVector.dqdv[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.dqdv(v, b); + shuntVector.dqdb[shuntNum] = ShuntCompensatorReactiveFlowEquationTerm.dqdb(v); + } + } } stopwatch.stop(); From fe607423e7c76e35fb74c62f7ac7863411eff29c Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 27 Jun 2023 21:05:06 +0200 Subject: [PATCH 17/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../powsybl/openloadflow/ac/equations/AcNetworkVector.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java index fe8732304f..07ec3dbaa0 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java @@ -523,6 +523,11 @@ public void onTapPositionChange(LfBranch branch, int oldPosition, int newPositio branchVector.r1[branch.getNum()] = piModel.getR1(); } + @Override + public void onShuntSusceptanceChange(LfShunt shunt, double b) { + shuntVector.b[shunt.getNum()] = b; + } + @Override public void onVariableChange(Variable variable, ChangeType changeType) { variablesInvalid = true; From d3b2b4d7f4b27a02ccf273833d5eea209fd77835 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 27 Jun 2023 21:40:14 +0200 Subject: [PATCH 18/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AsymmetricalAcEquationSystemCreator.java | 1 - .../network/impl/LfShuntImpl.java | 4 +-- .../powsybl/openloadflow/EquationsTest.java | 26 ++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index 7d9b884f95..3b25a0c440 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -9,7 +9,6 @@ package com.powsybl.openloadflow.ac.equations.asym; import com.powsybl.openloadflow.ac.equations.*; -import com.powsybl.openloadflow.equations.EquationSystem; import com.powsybl.openloadflow.equations.EquationTerm; import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.util.ComplexPart; 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 a13bdea1e0..9c9e3b5d0a 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/LfShuntImpl.java @@ -265,7 +265,7 @@ public void reInit() { throw new PowsyblException("Cannot re-init a shunt compensator with voltage control capabilities"); } List shuntCompensators = shuntCompensatorsRefs.stream().map(Ref::get).collect(Collectors.toList()); - b = computeB(shuntCompensators, zb); - g = computeG(shuntCompensators, zb); + setB(computeB(shuntCompensators, zb)); + setG(computeG(shuntCompensators, zb)); } } diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 64fa9840ca..6ff81dd50f 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -55,7 +55,7 @@ public Object answer(InvocationOnMock invocation) { private static final double A_1 = 0.324294234; private static final double V_2 = 1.0718794209362505; private static final double PH_2 = 0.18609589391040748; - private static final double B = 0.2748383993949494; + private static final double B_SHUNT = 0.2748383993949494; private static final double DROOP = 103.13240312354819; private static final double P_0 = 1.9280906677246095; private static final double LOSS_FACTOR_1 = 0.01100000023841858; @@ -264,10 +264,17 @@ void shuntTest() { var shunt = Mockito.mock(LfShunt.class, new RuntimeExceptionAnswer()); Mockito.doReturn(0).when(shunt).getNum(); Mockito.doReturn(G_SHUNT).when(shunt).getG(); + Mockito.doReturn(B_SHUNT).when(shunt).getB(); + Mockito.doReturn(true).when(shunt).hasVoltageControlCapability(); Mockito.doReturn(false).when(shunt).isDisabled(); var bus = Mockito.mock(LfBus.class, ANSWER); Mockito.doReturn(0).when(bus).getNum(); + Mockito.doReturn(bus).when(shunt).getBus(); + + LfNetwork network = Mockito.mock(LfNetwork.class); + Mockito.doReturn(List.of(shunt)).when(network).getShunts(); + Mockito.doReturn(List.of(bus)).when(network).getBuses(); VariableSet variableSet = new VariableSet<>(); var vVar = variableSet.getVariable(0, AcVariableType.BUS_V); @@ -279,12 +286,19 @@ void shuntTest() { bVar.setRow(1); unknownVar.setRow(2); - var sv = new StateVector(new double[] {V_1, B, 0}); + EquationSystem equationSystem = new EquationSystem<>(); + var sv = equationSystem.getStateVector(); + sv.set(new double[] {V_1, B_SHUNT, 0}); -// assertArrayEquals(new double[] {4.275919696380507E-5, 7.98163392892194E-5, Double.NaN, Double.NaN, Double.NaN}, -// eval(new ShuntCompensatorActiveFlowEquationTerm(shunt, bus, variableSet), variables, sv)); -// assertArrayEquals(new double[] {-0.3155098135679268, -0.588945539602459, -1.1479830120627779, Double.NaN, -1.7369285516652369}, -// eval(new ShuntCompensatorReactiveFlowEquationTerm(shunt, bus, variableSet, true), variables, sv)); + AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + networkVector.getBusVector().vRow[bus.getNum()] = vVar.getRow(); + networkVector.getShuntVector().bRow[shunt.getNum()] = bVar.getRow(); + networkVector.updatePowerFlows(); + assertArrayEquals(new double[] {4.275919696380507E-5, 7.98163392892194E-5, Double.NaN, Double.NaN, Double.NaN}, + eval(new ShuntCompensatorActiveFlowEquationTerm(networkVector.getShuntVector(), shunt.getNum(), bus.getNum(), variableSet), variables, sv)); + assertArrayEquals(new double[] {-0.3155098135679268, -0.588945539602459, -1.1479830120627779, Double.NaN, -1.7369285516652369}, + eval(new ShuntCompensatorReactiveFlowEquationTerm(networkVector.getShuntVector(), shunt.getNum(), bus.getNum(), variableSet, true), variables, sv)); } @Test From 52e69b432aea5146ea283c84064a25c4fd1e2b89 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sun, 8 Oct 2023 22:22:47 +0200 Subject: [PATCH 19/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcBranchVector.java | 19 +++++++++---------- .../powsybl/openloadflow/EquationsTest.java | 1 + 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java index 15b116d802..20a5036c9c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java @@ -155,16 +155,15 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter bus1Num[i] = bus1 != null ? bus1.getNum() : -1; bus2Num[i] = bus2 != null ? bus2.getNum() : -1; PiModel piModel = branch.getPiModel(); -// if (piModel.getR() == 0 && piModel.getX() == 0) { -// throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); -// } - y[i] = piModel.getY(); - // y12 = g12+j.b12 = 1/(r+j.x) - g12[i] = piModel.getR() * y[i] * y[i]; - b12[i] = -piModel.getX() * y[i] * y[i]; - ksi[i] = piModel.getKsi(); - cosKsi[i] = FastMath.cos(ksi[i]); - sinKsi[i] = FastMath.sin(ksi[i]); + if (piModel.getZ() != 0) { + y[i] = piModel.getY(); + // y12 = g12+j.b12 = 1/(r+j.x) + g12[i] = piModel.getR() * y[i] * y[i]; + b12[i] = -piModel.getX() * y[i] * y[i]; + ksi[i] = piModel.getKsi(); + cosKsi[i] = FastMath.cos(ksi[i]); + sinKsi[i] = FastMath.sin(ksi[i]); + } b1[i] = piModel.getB1(); b2[i] = piModel.getB2(); g1[i] = piModel.getG1(); diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index 6ff81dd50f..ecbba04f2d 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -112,6 +112,7 @@ void setUp() { Mockito.doReturn(R).when(piModel).getR(); Mockito.doReturn(X).when(piModel).getX(); Mockito.doReturn(Y).when(piModel).getY(); + Mockito.doReturn(1 / Y).when(piModel).getZ(); Mockito.doReturn(G_1).when(piModel).getG1(); Mockito.doReturn(G_2).when(piModel).getG2(); Mockito.doReturn(B_1).when(piModel).getB1(); From f95feadacacb016ef130656cb0d7aa31aec963f6 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 18 Oct 2023 16:01:11 +0200 Subject: [PATCH 20/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/ac/equations/AcEquationSystemCreator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 61a24f1909..b4ca7f75e4 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -65,14 +65,15 @@ protected void createBusEquation(LfBus bus, AcEquationSystemCreationContext crea } private void createLoadEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); for (LfLoad load : bus.getLoads()) { load.getLoadModel().ifPresent(loadModel -> { var p = new LoadModelActiveFlowEquationTerm(bus, loadModel, load, equationSystem.getVariableSet()); - creationContext.getEquationSystem().createEquation(bus, AcEquationType.BUS_TARGET_P) + equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_P) .addTerm(p); load.setP(p); var q = new LoadModelReactiveFlowEquationTerm(bus, loadModel, load, equationSystem.getVariableSet()); - creationContext.getEquationSystem().createEquation(bus, AcEquationType.BUS_TARGET_Q) + equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_Q) .addTerm(q); load.setQ(q); }); From 5e06ec6e007b234d41535395a773addae2dd79c6 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sat, 2 Dec 2023 10:16:08 +0100 Subject: [PATCH 21/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcEquationSystemCreator.java | 35 +++++++++++-------- .../AsymmetricalAcEquationSystemCreator.java | 4 +-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 8103786dee..eae8aec347 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -101,7 +101,7 @@ private void createGeneratorVoltageControlEquations(LfBus bus, AcEquationSystemC createGeneratorLocalVoltageControlEquation(bus, creationContext); } else { // create reactive power distribution equations at voltage controller buses - createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext); + createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext, creationParameters); } updateGeneratorVoltageControl(voltageControl, creationContext.getEquationSystem()); } @@ -125,16 +125,18 @@ private void createGeneratorLocalVoltageControlEquation(LfBus bus, AcEquationSys } } - protected void createGeneratorReactivePowerControlBranchEquation(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, + protected void createGeneratorReactivePowerControlBranchEquation(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext, boolean deriveA1, boolean deriveR1) { if (bus1 != null && bus2 != null) { + var equationSystem = creationContext.getEquationSystem(); + var branchVector = creationContext.getNetworkVector().getBranchVector(); branch.getGeneratorReactivePowerControl().ifPresent(rpc -> { EquationTerm q = rpc.getControlledSide() == TwoSides.ONE - ? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + ? new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1) + : new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_Q) .addTerm(q); - createGeneratorReactivePowerDistributionEquations(rpc, equationSystem, creationParameters); + createGeneratorReactivePowerDistributionEquations(rpc, creationContext, creationParameters); updateGeneratorReactivePowerControlBranchEquations(rpc, equationSystem); }); } @@ -208,7 +210,7 @@ private static void createShuntEquations(LfBus bus, AcEquationSystemCreationCont bus.getSvcShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, false, creationContext)); } - private static void createGeneratorReactivePowerDistributionEquations(Control control, EquationSystem equationSystem, + private static void createGeneratorReactivePowerDistributionEquations(Control control, AcEquationSystemCreationContext creationContext, AcEquationSystemCreationParameters creationParameters) { List controllerBuses = null; if (control instanceof GeneratorVoltageControl generatorVoltageControl) { @@ -219,6 +221,7 @@ private static void createGeneratorReactivePowerDistributionEquations(Control co throw new PowsyblException("Control has to be of type GeneratorVoltageControl or ReactivePowerControl to create Q distribution equations"); } + var equationSystem = creationContext.getEquationSystem(); for (LfBus controllerBus : controllerBuses) { // reactive power at controller bus i (supposing this voltage control is enabled) // q_i = qPercent_i * sum_j(q_j) where j are all the voltage controller buses @@ -226,12 +229,12 @@ private static void createGeneratorReactivePowerDistributionEquations(Control co // which can be rewritten in a more simple way // 0 = (qPercent_i - 1) * q_i + qPercent_i * sum_j(q_j) where j are all the voltage controller buses except i Equation zero = equationSystem.createEquation(controllerBus, AcEquationType.DISTR_Q) - .addTerms(createReactiveTerms(controllerBus, equationSystem.getVariableSet(), creationParameters).stream() + .addTerms(createReactiveTerms(controllerBus, creationContext, creationParameters).stream() .map(term -> term.multiply(() -> controllerBus.getRemoteControlReactivePercent() - 1)) .collect(Collectors.toList())); for (LfBus otherControllerBus : controllerBuses) { if (otherControllerBus != controllerBus) { - zero.addTerms(createReactiveTerms(otherControllerBus, equationSystem.getVariableSet(), creationParameters).stream() + zero.addTerms(createReactiveTerms(otherControllerBus, creationContext, creationParameters).stream() .map(term -> term.multiply(controllerBus::getRemoteControlReactivePercent)) .collect(Collectors.toList())); } @@ -247,7 +250,7 @@ public static void recreateReactivePowerDistributionEquations(GeneratorVoltageCo equationSystem.removeEquation(controllerBus.getNum(), AcEquationType.DISTR_Q); } if (!voltageControl.isLocalControl()) { - createGeneratorReactivePowerDistributionEquations(voltageControl, equationSystem, parameters); + createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext, parameters); } updateGeneratorVoltageControl(voltageControl, equationSystem); } @@ -459,8 +462,10 @@ private static void createNonImpedantBranch(LfBranch branch, LfBus bus1, LfBus b } } - protected static void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus1, LfBus bus2, AcBranchVector branchVector, - EquationSystem equationSystem, boolean deriveA1, boolean deriveR1) { + protected static void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext, + boolean deriveA1, boolean deriveR1) { + var equationSystem = creationContext.getEquationSystem(); + var branchVector = creationContext.getNetworkVector().getBranchVector(); if (deriveA1) { EquationTerm a1 = equationSystem.getVariable(branch.getNum(), AcVariableType.BRANCH_ALPHA1) .createTerm(); @@ -477,8 +482,8 @@ protected static void createTransformerPhaseControlEquations(LfBranch branch, Lf } EquationTerm p = phaseControl.getControlledSide() == TwoSides.ONE - ? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + ? new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1) + : new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_P) .addTerm(p) .setActive(false); // by default BRANCH_TARGET_ALPHA1 is active and BRANCH_TARGET_P inactive @@ -656,9 +661,9 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE createBranchEquations(branch, bus1, bus2, equationSystem, p1, q1, p2, q2, i1, i2); - createGeneratorReactivePowerControlBranchEquation(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createGeneratorReactivePowerControlBranchEquation(branch, bus1, bus2, creationContext, deriveA1, deriveR1); - createTransformerPhaseControlEquations(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); + createTransformerPhaseControlEquations(branch, bus1, bus2, creationContext, deriveA1, deriveR1); } protected static void createBranchEquations(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index cc0cb93582..0899543111 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -221,8 +221,8 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE .addTerm(iyn2); } - createGeneratorReactivePowerControlBranchEquation(branch, bus1, bus2, equationSystem, deriveA1, deriveR1); + createGeneratorReactivePowerControlBranchEquation(branch, bus1, bus2, creationContext, deriveA1, deriveR1); - createTransformerPhaseControlEquations(branch, bus1, bus2, branchVector, equationSystem, deriveA1, deriveR1); + createTransformerPhaseControlEquations(branch, bus1, bus2, creationContext, deriveA1, deriveR1); } } From 378b6de692203a4d995e57f9199017e76e3b3c77 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sun, 3 Dec 2023 13:54:30 +0100 Subject: [PATCH 22/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../AbstractBranchAcFlowEquationTerm.java | 55 +-- ...bstractClosedBranchAcFlowEquationTerm.java | 27 +- ...ractOpenSide1BranchAcFlowEquationTerm.java | 14 +- ...ractOpenSide2BranchAcFlowEquationTerm.java | 14 +- .../AbstractShuntCompensatorEquationTerm.java | 30 +- .../AcEquationSystemCreationContext.java | 9 +- .../ac/equations/AcEquationSystemCreator.java | 174 +++++---- .../ac/equations/AcEquationSystemUpdater.java | 13 +- .../ClosedBranchI1xFlowEquationTerm.java | 25 +- .../ClosedBranchI1yFlowEquationTerm.java | 19 +- .../ClosedBranchI2xFlowEquationTerm.java | 19 +- .../ClosedBranchI2yFlowEquationTerm.java | 19 +- ...osedBranchSide1ActiveFlowEquationTerm.java | 33 +- ...anchSide1CurrentMagnitudeEquationTerm.java | 28 +- ...edBranchSide1ReactiveFlowEquationTerm.java | 33 +- ...osedBranchSide2ActiveFlowEquationTerm.java | 33 +- ...anchSide2CurrentMagnitudeEquationTerm.java | 28 +- ...edBranchSide2ReactiveFlowEquationTerm.java | 33 +- ...OpenBranchSide1ActiveFlowEquationTerm.java | 19 +- ...anchSide1CurrentMagnitudeEquationTerm.java | 22 +- ...enBranchSide1ReactiveFlowEquationTerm.java | 19 +- ...OpenBranchSide2ActiveFlowEquationTerm.java | 23 +- ...anchSide2CurrentMagnitudeEquationTerm.java | 26 +- ...enBranchSide2ReactiveFlowEquationTerm.java | 23 +- ...huntCompensatorActiveFlowEquationTerm.java | 14 +- ...ntCompensatorReactiveFlowEquationTerm.java | 16 +- .../AsymmetricalAcEquationSystemCreator.java | 29 +- ...bstractBranchVectorAcFlowEquationTerm.java | 40 ++ ...tClosedBranchVectorAcFlowEquationTerm.java | 135 +++++++ ...enSide1BranchVectorAcFlowEquationTerm.java | 40 ++ ...enSide2BranchVectorAcFlowEquationTerm.java | 40 ++ ...actShuntVectorCompensatorEquationTerm.java | 50 +++ .../{ => vector}/AcBranchVector.java | 4 +- .../equations/{ => vector}/AcBusVector.java | 2 +- .../{ => vector}/AcNetworkVector.java | 7 +- .../equations/{ => vector}/AcShuntVector.java | 2 +- ...ctorizedEquationSystemCreationContext.java | 31 ++ .../AcVectorizedEquationSystemCreator.java | 113 ++++++ ...anchVectorSide1ActiveFlowEquationTerm.java | 84 +++++ ...ctorSide1CurrentMagnitudeEquationTerm.java | 84 +++++ ...chVectorSide1ReactiveFlowEquationTerm.java | 84 +++++ ...anchVectorSide2ActiveFlowEquationTerm.java | 83 +++++ ...ctorSide2CurrentMagnitudeEquationTerm.java | 84 +++++ ...chVectorSide2ReactiveFlowEquationTerm.java | 83 +++++ ...anchVectorSide1ActiveFlowEquationTerm.java | 47 +++ ...ctorSide1CurrentMagnitudeEquationTerm.java | 70 ++++ ...chVectorSide1ReactiveFlowEquationTerm.java | 47 +++ ...anchVectorSide2ActiveFlowEquationTerm.java | 47 +++ ...ctorSide2CurrentMagnitudeEquationTerm.java | 79 ++++ ...chVectorSide2ReactiveFlowEquationTerm.java | 47 +++ ...ctorCompensatorActiveFlowEquationTerm.java | 52 +++ ...orCompensatorReactiveFlowEquationTerm.java | 77 ++++ .../powsybl/openloadflow/EquationsTest.java | 114 ++---- .../openloadflow/VectorEquationsTest.java | 343 ++++++++++++++++++ .../network/impl/LfSwitchTest.java | 12 +- 55 files changed, 2231 insertions(+), 467 deletions(-) create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractBranchVectorAcFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractShuntVectorCompensatorEquationTerm.java rename src/main/java/com/powsybl/openloadflow/ac/equations/{ => vector}/AcBranchVector.java (96%) rename src/main/java/com/powsybl/openloadflow/ac/equations/{ => vector}/AcBusVector.java (93%) rename src/main/java/com/powsybl/openloadflow/ac/equations/{ => vector}/AcNetworkVector.java (98%) rename src/main/java/com/powsybl/openloadflow/ac/equations/{ => vector}/AcShuntVector.java (96%) create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreationContext.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorActiveFlowEquationTerm.java create mode 100644 src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java create mode 100644 src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java index 564edad132..16fa52d399 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java @@ -1,38 +1,43 @@ /** - * Copyright (c) 2021, RTE (http://www.rte-france.com) + * Copyright (c) 2019, 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.ac.equations; -import com.powsybl.openloadflow.equations.AbstractNamedEquationTerm; -import com.powsybl.openloadflow.network.ElementType; - -import java.util.Objects; +import com.powsybl.openloadflow.equations.AbstractElementEquationTerm; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.PiModel; /** * @author Geoffroy Jamgotchian {@literal } */ -public abstract class AbstractBranchAcFlowEquationTerm extends AbstractNamedEquationTerm { - - protected final AcBranchVector branchVector; - - protected final int num; - - protected AbstractBranchAcFlowEquationTerm(AcBranchVector branchVector, int num) { - super(!Objects.requireNonNull(branchVector).disabled[num]); - this.branchVector = Objects.requireNonNull(branchVector); - this.num = num; - } - - @Override - public ElementType getElementType() { - return ElementType.BRANCH; - } - - @Override - public int getElementNum() { - return num; +abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationTerm { + + protected final double b1; + protected final double b2; + protected final double g1; + protected final double g2; + protected final double y; + protected final double ksi; + protected final double g12; + protected final double b12; + + protected AbstractBranchAcFlowEquationTerm(LfBranch branch) { + super(branch); + PiModel piModel = branch.getPiModel(); + if (piModel.getR() == 0 && piModel.getX() == 0) { + throw new IllegalArgumentException("Non impedant branch not supported: " + branch.getId()); + } + b1 = piModel.getB1(); + b2 = piModel.getB2(); + g1 = piModel.getG1(); + g2 = piModel.getG2(); + y = piModel.getY(); + ksi = piModel.getKsi(); + // y12 = g12+j.b12 = 1/(r+j.x) + g12 = piModel.getR() * y * y; + b12 = -piModel.getX() * y * y; } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java index f9fea2148b..d5d401a17d 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java @@ -9,6 +9,8 @@ import com.powsybl.math.matrix.DenseMatrix; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import java.util.ArrayList; @@ -52,19 +54,20 @@ private static AcVariableType getVoltageAngleType(Fortescue.SequenceType sequenc }; } - protected AbstractClosedBranchAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum); + protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch); + Objects.requireNonNull(bus1); + Objects.requireNonNull(bus2); Objects.requireNonNull(variableSet); AcVariableType vType = getVoltageMagnitudeType(sequenceType); AcVariableType angleType = getVoltageAngleType(sequenceType); - v1Var = variableSet.getVariable(bus1Num, vType); - v2Var = variableSet.getVariable(bus2Num, vType); - ph1Var = variableSet.getVariable(bus1Num, angleType); - ph2Var = variableSet.getVariable(bus2Num, angleType); - a1Var = deriveA1 ? variableSet.getVariable(branchNum, AcVariableType.BRANCH_ALPHA1) : null; - r1Var = deriveR1 ? variableSet.getVariable(branchNum, AcVariableType.BRANCH_RHO1) : null; + v1Var = variableSet.getVariable(bus1.getNum(), vType); + v2Var = variableSet.getVariable(bus2.getNum(), vType); + ph1Var = variableSet.getVariable(bus1.getNum(), angleType); + ph2Var = variableSet.getVariable(bus2.getNum(), angleType); + a1Var = deriveA1 ? variableSet.getVariable(branch.getNum(), AcVariableType.BRANCH_ALPHA1) : null; + r1Var = deriveR1 ? variableSet.getVariable(branch.getNum(), AcVariableType.BRANCH_RHO1) : null; variables.add(v1Var); variables.add(v2Var); variables.add(ph1Var); @@ -98,11 +101,11 @@ protected double ph2() { } protected double r1() { - return r1Var != null ? sv.get(r1Var.getRow()) : branchVector.r1[num]; + return r1Var != null ? sv.get(r1Var.getRow()) : element.getPiModel().getR1(); } protected double a1() { - return a1Var != null ? sv.get(a1Var.getRow()) : branchVector.a1[num]; + return a1Var != null ? sv.get(a1Var.getRow()) : element.getPiModel().getA1(); } public static double theta1(double ksi, double ph1, double a1, double ph2) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java index 10a0d10d9d..55c2f5ba35 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import java.util.List; @@ -18,13 +20,13 @@ abstract class AbstractOpenSide1BranchAcFlowEquationTerm extends AbstractBranchA protected final List> variables; - protected AbstractOpenSide1BranchAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, - AcVariableType variableType, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum); - variables = List.of(variableSet.getVariable(bus2Num, variableType)); + protected AbstractOpenSide1BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType, + LfBus bus, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch); + variables = List.of(variableSet.getVariable(bus.getNum(), variableType)); if (deriveA1 || deriveR1) { - throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); + throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branch.getId()); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java index c93751d0ea..0f79825dc9 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide2BranchAcFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import java.util.List; @@ -18,13 +20,13 @@ abstract class AbstractOpenSide2BranchAcFlowEquationTerm extends AbstractBranchA protected final List> variables; - protected AbstractOpenSide2BranchAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, - AcVariableType variableType, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum); - variables = List.of(variableSet.getVariable(bus1Num, variableType)); + protected AbstractOpenSide2BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType, + LfBus bus, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch); + variables = List.of(variableSet.getVariable(bus.getNum(), variableType)); if (deriveA1 || deriveR1) { - throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); + throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branch.getId()); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java index 8c892c6a2d..a08aefc9cc 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractShuntCompensatorEquationTerm.java @@ -6,40 +6,26 @@ */ package com.powsybl.openloadflow.ac.equations; -import com.powsybl.openloadflow.equations.AbstractNamedEquationTerm; +import com.powsybl.openloadflow.equations.AbstractElementEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; -import com.powsybl.openloadflow.network.ElementType; +import com.powsybl.openloadflow.network.LfBus; +import com.powsybl.openloadflow.network.LfShunt; import java.util.Objects; /** * @author Geoffroy Jamgotchian {@literal } */ -public abstract class AbstractShuntCompensatorEquationTerm extends AbstractNamedEquationTerm { - - protected final AcShuntVector shuntVector; - - protected final int num; +public abstract class AbstractShuntCompensatorEquationTerm extends AbstractElementEquationTerm { protected final Variable vVar; - protected AbstractShuntCompensatorEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet) { - super(!Objects.requireNonNull(shuntVector).disabled[num]); - this.shuntVector = Objects.requireNonNull(shuntVector); - this.num = num; + protected AbstractShuntCompensatorEquationTerm(LfShunt shunt, LfBus bus, VariableSet variableSet) { + super(shunt); + Objects.requireNonNull(bus); Objects.requireNonNull(variableSet); - vVar = variableSet.getVariable(busNum, AcVariableType.BUS_V); - } - - @Override - public ElementType getElementType() { - return ElementType.SHUNT_COMPENSATOR; - } - - @Override - public int getElementNum() { - return num; + vVar = variableSet.getVariable(bus.getNum(), AcVariableType.BUS_V); } protected double v() { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java index c2b87984f9..5b5f1056e1 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreationContext.java @@ -17,18 +17,11 @@ public class AcEquationSystemCreationContext { private final EquationSystem equationSystem; - private final AcNetworkVector networkVector; - - public AcEquationSystemCreationContext(EquationSystem equationSystem, AcNetworkVector networkVector) { + public AcEquationSystemCreationContext(EquationSystem equationSystem) { this.equationSystem = Objects.requireNonNull(equationSystem); - this.networkVector = Objects.requireNonNull(networkVector); } public EquationSystem getEquationSystem() { return equationSystem; } - - public AcNetworkVector getNetworkVector() { - return networkVector; - } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index eae8aec347..1b422eedfb 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -129,11 +129,10 @@ protected void createGeneratorReactivePowerControlBranchEquation(LfBranch branch boolean deriveA1, boolean deriveR1) { if (bus1 != null && bus2 != null) { var equationSystem = creationContext.getEquationSystem(); - var branchVector = creationContext.getNetworkVector().getBranchVector(); branch.getGeneratorReactivePowerControl().ifPresent(rpc -> { EquationTerm q = rpc.getControlledSide() == TwoSides.ONE - ? new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + ? createClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext) + : createClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_Q) .addTerm(q); createGeneratorReactivePowerDistributionEquations(rpc, creationContext, creationParameters); @@ -193,25 +192,32 @@ public static void updateGeneratorReactivePowerControlBranchEquations(GeneratorR } } - private static void createShuntEquation(LfShunt shunt, LfBus bus, boolean deriveB, AcEquationSystemCreationContext creationContext) { - AcShuntVector shuntVector = creationContext.getNetworkVector().getShuntVector(); + private void createShuntEquation(LfShunt shunt, LfBus bus, boolean deriveB, AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); - ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shuntVector, shunt.getNum(), bus.getNum(), equationSystem.getVariableSet(), deriveB); + var q = createShuntCompensatorReactiveFlowEquationTerm(shunt, bus, deriveB, creationContext); equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_Q).addTerm(q); shunt.setQ(q); - ShuntCompensatorActiveFlowEquationTerm p = new ShuntCompensatorActiveFlowEquationTerm(shuntVector, shunt.getNum(), bus.getNum(), equationSystem.getVariableSet()); + var p = createShuntCompensatorActiveFlowEquationTerm(shunt, bus, creationContext); equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_P).addTerm(p); shunt.setP(p); } - private static void createShuntEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { + protected EquationTerm createShuntCompensatorActiveFlowEquationTerm(LfShunt shunt, LfBus bus, AcEquationSystemCreationContext creationContext) { + return new ShuntCompensatorActiveFlowEquationTerm(shunt, bus, creationContext.getEquationSystem().getVariableSet()); + } + + protected EquationTerm createShuntCompensatorReactiveFlowEquationTerm(LfShunt shunt, LfBus bus, boolean deriveB, AcEquationSystemCreationContext creationContext) { + return new ShuntCompensatorReactiveFlowEquationTerm(shunt, bus, creationContext.getEquationSystem().getVariableSet(), deriveB); + } + + private void createShuntEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { bus.getShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, false, creationContext)); bus.getControllerShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, shunt.hasVoltageControlCapability(), creationContext)); bus.getSvcShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, false, creationContext)); } - private static void createGeneratorReactivePowerDistributionEquations(Control control, AcEquationSystemCreationContext creationContext, - AcEquationSystemCreationParameters creationParameters) { + private void createGeneratorReactivePowerDistributionEquations(Control control, AcEquationSystemCreationContext creationContext, + AcEquationSystemCreationParameters creationParameters) { List controllerBuses = null; if (control instanceof GeneratorVoltageControl generatorVoltageControl) { controllerBuses = generatorVoltageControl.getMergedControllerElements(); @@ -242,15 +248,14 @@ private static void createGeneratorReactivePowerDistributionEquations(Control co } } - public static void recreateReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, - AcEquationSystemCreationContext creationContext, - AcEquationSystemCreationParameters parameters) { + public void recreateReactivePowerDistributionEquations(GeneratorVoltageControl voltageControl, + AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); for (LfBus controllerBus : voltageControl.getMergedControllerElements()) { equationSystem.removeEquation(controllerBus.getNum(), AcEquationType.DISTR_Q); } if (!voltageControl.isLocalControl()) { - createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext, parameters); + createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext, creationParameters); } updateGeneratorVoltageControl(voltageControl, equationSystem); } @@ -323,11 +328,10 @@ static void updateRemoteVoltageControlEquations(VoltageCon } } - private static List> createReactiveTerms(LfBus controllerBus, - AcEquationSystemCreationContext creationContext, - AcEquationSystemCreationParameters creationParameters) { + private List> createReactiveTerms(LfBus controllerBus, + AcEquationSystemCreationContext creationContext, + AcEquationSystemCreationParameters creationParameters) { var variableSet = creationContext.getEquationSystem().getVariableSet(); - var branchVector = creationContext.getNetworkVector().getBranchVector(); List> terms = new ArrayList<>(); for (LfBranch branch : controllerBus.getBranches()) { EquationTerm q; @@ -346,23 +350,22 @@ private static List> createReactive boolean deriveR1 = isDeriveR1(branch); if (branch.getBus1() == controllerBus) { LfBus otherSideBus = branch.getBus2(); - q = otherSideBus != null ? new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), controllerBus.getNum(), otherSideBus.getNum(), variableSet, deriveA1, deriveR1) - : new OpenBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), controllerBus.getNum(), variableSet, deriveA1, deriveR1); + q = otherSideBus != null ? createClosedBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, otherSideBus, deriveA1, deriveR1, creationContext) + : createOpenBranchSide2ReactiveFlowEquationTerm(branch, controllerBus, deriveA1, deriveR1, creationContext); } else { LfBus otherSideBus = branch.getBus1(); - q = otherSideBus != null ? new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), otherSideBus.getNum(), controllerBus.getNum(), variableSet, deriveA1, deriveR1) - : new OpenBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), controllerBus.getNum(), variableSet, deriveA1, deriveR1); + q = otherSideBus != null ? createClosedBranchSide2ReactiveFlowEquationTerm(branch, otherSideBus, controllerBus, deriveA1, deriveR1, creationContext) + : createOpenBranchSide1ReactiveFlowEquationTerm(branch, controllerBus, deriveA1, deriveR1, creationContext); } } terms.add(q); } - AcShuntVector shuntVector = creationContext.getNetworkVector().getShuntVector(); controllerBus.getShunt().ifPresent(shunt -> { - ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shuntVector, shunt.getNum(), controllerBus.getNum(), variableSet, false); + var q = createShuntCompensatorReactiveFlowEquationTerm(shunt, controllerBus, false, creationContext); terms.add(q); }); controllerBus.getControllerShunt().ifPresent(shunt -> { - ShuntCompensatorReactiveFlowEquationTerm q = new ShuntCompensatorReactiveFlowEquationTerm(shuntVector, shunt.getNum(), controllerBus.getNum(), variableSet, false); + var q = createShuntCompensatorReactiveFlowEquationTerm(shunt, controllerBus, false, creationContext); terms.add(q); }); return terms; @@ -462,10 +465,9 @@ private static void createNonImpedantBranch(LfBranch branch, LfBus bus1, LfBus b } } - protected static void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext, - boolean deriveA1, boolean deriveR1) { + protected void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext, + boolean deriveA1, boolean deriveR1) { var equationSystem = creationContext.getEquationSystem(); - var branchVector = creationContext.getNetworkVector().getBranchVector(); if (deriveA1) { EquationTerm a1 = equationSystem.getVariable(branch.getNum(), AcVariableType.BRANCH_ALPHA1) .createTerm(); @@ -482,8 +484,8 @@ protected static void createTransformerPhaseControlEquations(LfBranch branch, Lf } EquationTerm p = phaseControl.getControlledSide() == TwoSides.ONE - ? new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1) - : new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + ? createClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext) + : createClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_P) .addTerm(p) .setActive(false); // by default BRANCH_TARGET_ALPHA1 is active and BRANCH_TARGET_P inactive @@ -622,18 +624,17 @@ public static void recreateShuntSusceptanceDistributionEquations(ShuntVoltageCon updateShuntVoltageControlEquations(voltageControl, equationSystem); } - protected static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParameters creationParameters) { + public static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParameters creationParameters) { return branch.isPhaseController() || creationParameters.isForceA1Var() && branch.hasPhaseControllerCapability() && branch.isConnectedAtBothSides(); } - protected static boolean isDeriveR1(LfBranch branch) { + public static boolean isDeriveR1(LfBranch branch) { return branch.isVoltageController(); } protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); - var branchVector = creationContext.getNetworkVector().getBranchVector(); EquationTerm p1 = null; EquationTerm q1 = null; EquationTerm p2 = null; @@ -643,20 +644,20 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE boolean deriveA1 = isDeriveA1(branch, creationParameters); boolean deriveR1 = isDeriveR1(branch); if (bus1 != null && bus2 != null) { - p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = createClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + q1 = createClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + p2 = createClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + q2 = createClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + i1 = createClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + i2 = createClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); } else if (bus1 != null) { - p1 = new OpenBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - q1 = new OpenBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - i1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = createOpenBranchSide2ActiveFlowEquationTerm(branch, bus1, deriveA1, deriveR1, creationContext); + q1 = createOpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, deriveA1, deriveR1, creationContext); + i1 = createOpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, deriveA1, deriveR1, creationContext); } else if (bus2 != null) { - p2 = new OpenBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - q2 = new OpenBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + p2 = createOpenBranchSide1ActiveFlowEquationTerm(branch, bus2, deriveA1, deriveR1, creationContext); + q2 = createOpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, deriveA1, deriveR1, creationContext); + i2 = createOpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, deriveA1, deriveR1, creationContext); } createBranchEquations(branch, bus1, bus2, equationSystem, p1, q1, p2, q2, i1, i2); @@ -666,6 +667,54 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE createTransformerPhaseControlEquations(branch, bus1, bus2, creationContext, deriveA1, deriveR1); } + protected EquationTerm createClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createOpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createOpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createOpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createOpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createOpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + protected EquationTerm createOpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + protected static void createBranchEquations(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, EquationTerm p1, EquationTerm q1, EquationTerm p2, EquationTerm q2, @@ -745,8 +794,9 @@ private void createBranchesEquations(AcEquationSystemCreationContext creationCon } } - private List> createActiveInjectionTerms(LfBus bus, AcBranchVector branchVector, - VariableSet variableSet) { + private List> createActiveInjectionTerms(LfBus bus, + AcEquationSystemCreationContext creationContext) { + var variableSet = creationContext.getEquationSystem().getVariableSet(); List> terms = new ArrayList<>(); for (LfBranch branch : bus.getBranches()) { if (branch.isZeroImpedance(LoadFlowModel.AC)) { @@ -762,13 +812,13 @@ private List> createActiveInjection boolean deriveR1 = isDeriveR1(branch); if (branch.getBus1() == bus) { LfBus otherSideBus = branch.getBus2(); - var p = otherSideBus != null ? new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus.getNum(), otherSideBus.getNum(), variableSet, deriveA1, deriveR1) - : new OpenBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus.getNum(), variableSet, deriveA1, deriveR1); + var p = otherSideBus != null ? createClosedBranchSide1ActiveFlowEquationTerm(branch, bus, otherSideBus, deriveA1, deriveR1, creationContext) + : createOpenBranchSide2ActiveFlowEquationTerm(branch, bus, deriveA1, deriveR1, creationContext); terms.add(p); } else { LfBus otherSideBus = branch.getBus1(); - var p = otherSideBus != null ? new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), otherSideBus.getNum(), bus.getNum(), variableSet, deriveA1, deriveR1) - : new OpenBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus.getNum(), variableSet, deriveA1, deriveR1); + var p = otherSideBus != null ? createClosedBranchSide2ActiveFlowEquationTerm(branch, otherSideBus, bus, deriveA1, deriveR1, creationContext) + : createOpenBranchSide1ActiveFlowEquationTerm(branch, bus, deriveA1, deriveR1, creationContext); terms.add(p); } } @@ -778,7 +828,6 @@ private List> createActiveInjection private void createMultipleSlackBusesEquations(AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); - var branchVector = creationContext.getNetworkVector().getBranchVector(); List slackBuses = network.getSlackBuses(); if (slackBuses.size() > 1) { LfBus firstSlackBus = slackBuses.get(0); @@ -788,24 +837,20 @@ private void createMultipleSlackBusesEquations(AcEquationSystemCreationContext c // 0 = slack_p1 - slack_p2 // 0 = slack_p1 - slack_p3 equationSystem.createEquation(slackBus, AcEquationType.BUS_DISTR_SLACK_P) - .addTerms(createActiveInjectionTerms(firstSlackBus, branchVector, equationSystem.getVariableSet())) - .addTerms(createActiveInjectionTerms(slackBus, branchVector, equationSystem.getVariableSet()).stream() + .addTerms(createActiveInjectionTerms(firstSlackBus, creationContext)) + .addTerms(createActiveInjectionTerms(slackBus, creationContext).stream() .map(EquationTerm::minus) .collect(Collectors.toList())); } } } - public EquationSystem create() { - EquationSystem equationSystem = new EquationSystem<>(); - - AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); - AcEquationSystemCreationContext creationContext = new AcEquationSystemCreationContext(equationSystem, networkVector); - + protected void create(AcEquationSystemCreationContext creationContext) { createBusesEquations(creationContext); createMultipleSlackBusesEquations(creationContext); createBranchesEquations(creationContext); + var equationSystem = creationContext.getEquationSystem(); for (LfHvdc hvdc : network.getHvdcs()) { createHvdcAcEmulationEquations(hvdc, equationSystem); } @@ -814,10 +859,13 @@ public EquationSystem create() { EquationSystemPostProcessor.findAll().forEach(pp -> pp.onCreate(equationSystem)); - network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(networkVector, equationSystem, creationParameters))); - - networkVector.startListening(); + network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(equationSystem, this))); + } + public EquationSystem create() { + EquationSystem equationSystem = new EquationSystem<>(); + AcEquationSystemCreationContext creationContext = new AcEquationSystemCreationContext(equationSystem); + create(creationContext); return equationSystem; } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java index 045e67a7df..43cb41ec05 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java @@ -18,15 +18,12 @@ */ public class AcEquationSystemUpdater extends AbstractEquationSystemUpdater { - private final AcNetworkVector networkVector; + private final AcEquationSystemCreator creator; - private final AcEquationSystemCreationParameters parameters; - - public AcEquationSystemUpdater(AcNetworkVector networkVector, EquationSystem equationSystem, - AcEquationSystemCreationParameters parameters) { + public AcEquationSystemUpdater(EquationSystem equationSystem, + AcEquationSystemCreator creator) { super(equationSystem, LoadFlowModel.AC); - this.networkVector = Objects.requireNonNull(networkVector); - this.parameters = Objects.requireNonNull(parameters); + this.creator = Objects.requireNonNull(creator); } private void updateVoltageControls(LfBus bus) { @@ -130,7 +127,7 @@ private void recreateDistributionEquations(LfZeroImpedanceNetwork network) { for (LfBus bus : network.getGraph().vertexSet()) { bus.getGeneratorVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateReactivePowerDistributionEquations(voltageControl, new AcEquationSystemCreationContext(equationSystem, networkVector), parameters)); + .ifPresent(voltageControl -> creator.recreateReactivePowerDistributionEquations(voltageControl, new AcEquationSystemCreationContext(equationSystem))); bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, equationSystem)); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java index 7fb5a84782..c9fea8a908 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1xFlowEquationTerm.java @@ -10,6 +10,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,10 +23,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI1xFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI1xFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI1xFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -42,15 +43,15 @@ public static double i1x(double g1, double b1, double v1, double ph1, double v2, return (g1 + g12) * v1 * FastMath.cos(ph1) - (b1 + b12) * v1 * FastMath.sin(ph1) - g12 * v2 * FastMath.cos(ph2) + b12 * v2 * FastMath.sin(ph2); } - private static double di1xdv1(double g1, double b1, double ph1, double g12, double b12) { + public static double di1xdv1(double g1, double b1, double ph1, double g12, double b12) { return (g1 + g12) * FastMath.cos(ph1) - (b1 + b12) * FastMath.sin(ph1); } - private static double di1xdv2(double ph2, double g12, double b12) { + public static double di1xdv2(double ph2, double g12, double b12) { return -g12 * FastMath.cos(ph2) + b12 * FastMath.sin(ph2); } - private static double di1xdph1(double g1, double b1, double v1, double ph1, double g12, double b12) { + public static double di1xdph1(double g1, double b1, double v1, double ph1, double g12, double b12) { return -(g1 + g12) * v1 * FastMath.sin(ph1) - (b1 + b12) * v1 * FastMath.cos(ph1); } @@ -60,20 +61,20 @@ private static double di1xdph2(double v2, double ph2, double g12, double b12) { @Override public double eval() { - return i1x(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return i1x(g1, b1, v1(), ph1(), v2(), ph2(), g12, b12); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di1xdv1(branchVector.g1[num], branchVector.b1[num], ph1(), branchVector.g12[num], branchVector.b12[num]); + return di1xdv1(g1, b1, ph1(), g12, b12); } else if (variable.equals(v2Var)) { - return di1xdv2(ph2(), branchVector.g12[num], branchVector.b12[num]); + return di1xdv2(ph2(), g12, b12); } else if (variable.equals(ph1Var)) { - return di1xdph1(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); + return di1xdph1(g1, b1, v1(), ph1(), g12, b12); } else if (variable.equals(ph2Var)) { - return di1xdph2(v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return di1xdph2(v2(), ph2(), g12, b12); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java index f6d77a3b79..8418373a14 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI1yFlowEquationTerm.java @@ -10,6 +10,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,10 +23,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI1yFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI1yFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI1yFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -60,20 +61,20 @@ private static double di1ydph2(double v2, double ph2, double g12, double b12) { @Override public double eval() { - return i1y(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return i1y(g1, b1, v1(), ph1(), v2(), ph2(), g12, b12); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di1ydv1(branchVector.g1[num], branchVector.b1[num], ph1(), branchVector.g12[num], branchVector.b12[num]); + return di1ydv1(g1, b1, ph1(), g12, b12); } else if (variable.equals(v2Var)) { - return di1ydv2(ph2(), branchVector.g12[num], branchVector.b12[num]); + return di1ydv2(ph2(), g12, b12); } else if (variable.equals(ph1Var)) { - return di1ydph1(branchVector.g1[num], branchVector.b1[num], v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); + return di1ydph1(g1, b1, v1(), ph1(), g12, b12); } else if (variable.equals(ph2Var)) { - return di1ydph2(v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return di1ydph2(v2(), ph2(), g12, b12); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java index 55cfe7c0f2..f7b684f3e4 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2xFlowEquationTerm.java @@ -10,6 +10,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,10 +23,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI2xFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI2xFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI2xFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -70,20 +71,20 @@ private static double di2xdph2(double g2, double b2, double v2, double ph2, doub @Override public double eval() { - return i2x(branchVector.g2[num], branchVector.b2[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return i2x(g2, b2, v1(), ph1(), v2(), ph2(), g12, b12); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di2xdv1(ph1(), branchVector.g12[num], branchVector.b12[num]); + return di2xdv1(ph1(), g12, b12); } else if (variable.equals(v2Var)) { - return di2xdv2(branchVector.g2[num], branchVector.b2[num], ph2(), branchVector.g12[num], branchVector.b12[num]); + return di2xdv2(g2, b2, ph2(), g12, b12); } else if (variable.equals(ph1Var)) { - return di2xdph1(v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); + return di2xdph1(v1(), ph1(), g12, b12); } else if (variable.equals(ph2Var)) { - return di2xdph2(branchVector.g2[num], branchVector.b2[num], v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return di2xdph2(g2, b2, v2(), ph2(), g12, b12); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java index 3c012cc3c3..670c551bcd 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchI2yFlowEquationTerm.java @@ -10,6 +10,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,10 +23,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchI2yFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchI2yFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchI2yFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } public double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { @@ -70,20 +71,20 @@ private static double di2ydph2(double g2, double b2, double v2, double ph2, doub @Override public double eval() { - return i2y(branchVector.g2[num], branchVector.b2[num], v1(), ph1(), v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return i2y(g2, b2, v1(), ph1(), v2(), ph2(), g12, b12); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return di2ydv1(ph1(), branchVector.g12[num], branchVector.b12[num]); + return di2ydv1(ph1(), g12, b12); } else if (variable.equals(v2Var)) { - return di2ydv2(branchVector.g2[num], branchVector.b2[num], ph2(), branchVector.g12[num], branchVector.b12[num]); + return di2ydv2(g2, b2, ph2(), g12, b12); } else if (variable.equals(ph1Var)) { - return di2ydph1(v1(), ph1(), branchVector.g12[num], branchVector.b12[num]); + return di2ydph1(v1(), ph1(), g12, b12); } else if (variable.equals(ph2Var)) { - return di2ydph2(branchVector.g2[num], branchVector.b2[num], v2(), ph2(), branchVector.g12[num], branchVector.b12[num]); + return di2ydph2(g2, b2, v2(), ph2(), g12, b12); } else { throw new IllegalStateException("Unexpected variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index cfba5d6afc..d7e08ca128 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,21 +23,17 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1ActiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + public ClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g1 = branchVector.g1[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); @@ -81,24 +79,25 @@ public static double dp1dr1(double y, double sinKsi, double g1, double v1, doubl @Override public double eval() { - return branchVector.p1[num]; + return p1(y, FastMath.sin(ksi), g1, v1(), r1(), v2(), FastMath.sin(theta1(ksi, ph1(), a1(), ph2()))); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double theta = theta1(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return branchVector.dp1dv1[num]; + return dp1dv1(y, FastMath.sin(ksi), g1, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(v2Var)) { - return branchVector.dp1dv2[num]; + return dp1dv2(y, v1(), r1(), FastMath.sin(theta)); } else if (variable.equals(ph1Var)) { - return branchVector.dp1dph1[num]; + return dp1dph1(y, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(ph2Var)) { - return branchVector.dp1dph2[num]; + return dp1dph2(y, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(a1Var)) { - return branchVector.dp1da1[num]; + return dp1da1(y, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(r1Var)) { - return branchVector.dp1dr1[num]; + return dp1dr1(y, FastMath.sin(ksi), g1, v1(), r1(), v2(), FastMath.sin(theta)); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index 4b555d6bae..4a76b0563b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -22,9 +24,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1CurrentMagnitudeEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + public ClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } @Override @@ -32,10 +34,6 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 if (dr1 != 0) { throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); } - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g1 = branchVector.g1[num]; - double b1 = branchVector.b1[num]; double v1 = v1(); double ph1 = ph1(); double r1 = r1(); @@ -110,42 +108,38 @@ private static double dimI1dph2(double y, double ksi, double r1, double a1, doub return r1 * (-y * R2 * v2 * FastMath.sin(theta(ksi, a1, ph2))); } - private static double di1dv1(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di1dv1(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, a1, ph2); return (reI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dreI1dv1(y, ksi, g1, b1, ph1, r1) + imI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dimI1dv1(y, ksi, g1, b1, ph1, r1)) / i1(y, ksi, g1, b1, v1, ph1, r1, v2, theta); } - private static double di1dv2(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di1dv2(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, a1, ph2); return (reI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dreI1dv2(y, ksi, r1, a1, ph2) + imI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dimI1dv2(y, ksi, r1, a1, ph2)) / i1(y, ksi, g1, b1, v1, ph1, r1, v2, theta); } - private static double di1dph1(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di1dph1(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, a1, ph2); return (reI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dreI1dph1(y, ksi, g1, b1, v1, ph1, r1) + imI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dimI1dph1(y, ksi, g1, b1, v1, ph1, r1)) / i1(y, ksi, g1, b1, v1, ph1, r1, v2, theta); } - private static double di1dph2(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di1dph2(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, a1, ph2); return (reI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dreI1dph2(y, ksi, r1, a1, v2, ph2) + imI1(y, ksi, g1, b1, v1, ph1, r1, v2, theta) * dimI1dph2(y, ksi, r1, a1, v2, ph2)) / i1(y, ksi, g1, b1, v1, ph1, r1, v2, theta); } - private static double di1da1(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di1da1(double y, double ksi, double g1, double b1, double v1, double ph1, double r1, double a1, double v2, double ph2) { return -di1dph2(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2); } @Override public double eval() { - return branchVector.i1[num]; + return i1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g1 = branchVector.g1[num]; - double b1 = branchVector.b1[num]; if (variable.equals(v1Var)) { return di1dv1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); } else if (variable.equals(v2Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index 278c656b22..d72fe38c7c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,21 +23,17 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide1ReactiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double b1 = branchVector.b1[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); @@ -83,24 +81,25 @@ public static double dq1dr1(double y, double cosKsi, double b1, double v1, doubl @Override public double eval() { - return branchVector.q1[num]; + return q1(y, FastMath.cos(ksi), b1, v1(), r1(), v2(), FastMath.cos(theta1(ksi, ph1(), a1(), ph2()))); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double theta = theta1(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return branchVector.dq1dv1[num]; + return dq1dv1(y, FastMath.cos(ksi), b1, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(v2Var)) { - return branchVector.dq1dv2[num]; + return dq1dv2(y, v1(), r1(), FastMath.cos(theta)); } else if (variable.equals(ph1Var)) { - return branchVector.dq1dph1[num]; + return dq1dph1(y, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(ph2Var)) { - return branchVector.dq1dph2[num]; + return dq1dph2(y, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(a1Var)) { - return branchVector.dq1da1[num]; + return dq1da1(y, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(r1Var)) { - return branchVector.dq1dr1[num]; + return dq1dr1(y, FastMath.cos(ksi), b1, v1(), r1(), v2(), FastMath.cos(theta)); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index 2eb66fcd96..2dd4c5b5b5 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,21 +23,17 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2ActiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g2 = branchVector.g2[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); @@ -80,24 +78,25 @@ public static double dp2dr1(double y, double v1, double v2, double sinTheta) { @Override public double eval() { - return branchVector.p2[num]; + return p2(y, FastMath.sin(ksi), g2, v1(), r1(), v2(), FastMath.sin(theta2(ksi, ph1(), a1(), ph2()))); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double theta = theta2(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return branchVector.dp2dv1[num]; + return dp2dv1(y, r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(v2Var)) { - return branchVector.dp2dv2[num]; + return dp2dv2(y, FastMath.sin(ksi), g2, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(ph1Var)) { - return branchVector.dp2dph1[num]; + return dp2dph1(y, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(ph2Var)) { - return branchVector.dp2dph2[num]; + return dp2dph2(y, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(a1Var)) { - return branchVector.dp2da1[num]; + return dp2da1(y, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(r1Var)) { - return branchVector.dp2dr1[num]; + return dp2dr1(y, v1(), v2(), FastMath.sin(theta)); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java index 86acb09df6..6567ec4f9d 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -22,9 +24,9 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2CurrentMagnitudeEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + public ClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } @Override @@ -32,10 +34,6 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 if (dr1 != 0) { throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); } - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g2 = branchVector.g2[num]; - double b2 = branchVector.b2[num]; double v1 = v1(); double ph1 = ph1(); double r1 = r1(); @@ -110,42 +108,38 @@ private static double dimI2dph1(double y, double v1, double r1, double theta) { return R2 * (-y * r1 * v1 * FastMath.sin(theta)); } - private static double di2dv2(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di2dv2(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, ph1, a1); return (reI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dreI2dv2(y, ksi, g2, b2, ph2) + imI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dimI2dv2(y, ksi, g2, b2, ph2)) / i2(y, ksi, g2, b2, v1, r1, v2, ph2, theta); } - private static double di2dv1(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di2dv1(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, ph1, a1); return (reI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dreI2dv1(y, r1, theta) + imI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dimI2dv1(y, ksi, ph1, r1, a1)) / i2(y, ksi, g2, b2, v1, r1, v2, ph2, theta); } - private static double di2dph2(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di2dph2(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, ph1, a1); return (reI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dreI2dph2(y, ksi, g2, b2, v2, ph2) + imI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dimI2dph2(y, ksi, g2, b2, v2, ph2)) / i2(y, ksi, g2, b2, v1, r1, v2, ph2, theta); } - private static double di2dph1(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di2dph1(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { double theta = theta(ksi, ph1, a1); return (reI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dreI2dph1(y, ksi, v1, ph1, r1, a1) + imI2(y, ksi, g2, b2, v1, r1, v2, ph2, theta) * dimI2dph1(y, v1, r1, theta)) / i2(y, ksi, g2, b2, v1, r1, v2, ph2, theta); } - private static double di2da1(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { + public static double di2da1(double y, double ksi, double g2, double b2, double v1, double ph1, double r1, double a1, double v2, double ph2) { return di2dph1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2); } @Override public double eval() { - return branchVector.i2[num]; + return i2(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g2 = branchVector.g2[num]; - double b2 = branchVector.b2[num]; if (variable.equals(v1Var)) { return di2dv1(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); } else if (variable.equals(v2Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index 1050129578..46bffea34a 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.util.Fortescue; import net.jafama.FastMath; @@ -21,21 +23,17 @@ @SuppressWarnings("squid:S00107") public class ClosedBranchSide2ReactiveFlowEquationTerm extends AbstractClosedBranchAcFlowEquationTerm { - public ClosedBranchSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { + super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double b2 = branchVector.b2[num]; double v1 = v1(); double r1 = r1(); double v2 = v2(); @@ -80,24 +78,25 @@ public static double dq2dr1(double y, double v1, double v2, double cosTheta) { @Override public double eval() { - return branchVector.q2[num]; + return q2(y, FastMath.cos(ksi), b2, v1(), r1(), v2(), FastMath.cos(theta2(ksi, ph1(), a1(), ph2()))); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); + double theta = theta2(ksi, ph1(), a1(), ph2()); if (variable.equals(v1Var)) { - return branchVector.dq2dv1[num]; + return dq2dv1(y, r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(v2Var)) { - return branchVector.dq2dv2[num]; + return dq2dv2(y, FastMath.cos(ksi), b2, v1(), r1(), v2(), FastMath.cos(theta)); } else if (variable.equals(ph1Var)) { - return branchVector.dq2dph1[num]; + return dq2dph1(y, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(ph2Var)) { - return branchVector.dq2dph2[num]; + return dq2dph2(y, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(a1Var)) { - return branchVector.dq2da1[num]; + return dq2da1(y, v1(), r1(), v2(), FastMath.sin(theta)); } else if (variable.equals(r1Var)) { - return branchVector.dq2dr1[num]; + return dq2dr1(y, v1(), v2(), FastMath.cos(theta)); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java index 0edfd57eed..5b26c3c70d 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ActiveFlowEquationTerm.java @@ -8,6 +8,9 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; +import net.jafama.FastMath; import java.util.Objects; @@ -20,10 +23,14 @@ public class OpenBranchSide1ActiveFlowEquationTerm extends AbstractOpenSide1Bran private final Variable v2Var; - public OpenBranchSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); - v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + public OpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus2, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); + } + + private double v2() { + return sv.get(v2Var.getRow()); } public static double p2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double v2) { @@ -38,14 +45,14 @@ public static double dp2dv2(double y, double cosKsi, double sinKsi, double g1, d @Override public double eval() { - return branchVector.p2[num]; + return p2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, v2()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v2Var)) { - return branchVector.dp2dv2[num]; + return dp2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, v2()); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java index e2863bb89c..8d1ce67204 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1CurrentMagnitudeEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -24,11 +26,11 @@ public class OpenBranchSide1CurrentMagnitudeEquationTerm extends AbstractOpenSid private final Variable ph2Var; - public OpenBranchSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); - v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); - ph2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_PHI); + public OpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus2, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); + ph2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_PHI); } private double v2() { @@ -71,25 +73,19 @@ private static double dimI2dv2(double y, double cosKsi, double sinKsi, double g1 return R2 * R2 * (gres(y, sinKsi, g1, b1, g2, shunt) * FastMath.sin(ph2) + bres(y, cosKsi, g1, b1, b2, shunt) * FastMath.cos(ph2)); } - private static double di2dv2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double b2, double v2, double ph2) { + public static double di2dv2(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double b2, double v2, double ph2) { return (reI2(y, cosKsi, sinKsi, g1, b1, g2, b2, v2, ph2) * dreI2dv2(y, cosKsi, sinKsi, g1, b1, g2, b2, ph2) + imI2(y, cosKsi, sinKsi, g1, b1, g2, b2, v2, ph2) * dimI2dv2(y, cosKsi, sinKsi, g1, b1, g2, b2, ph2)) / i2(y, cosKsi, sinKsi, g1, b1, g2, b2, v2, ph2); } @Override public double eval() { - return branchVector.i2[num]; + return i2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v2(), ph2()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g1 = branchVector.g1[num]; - double b1 = branchVector.b1[num]; - double g2 = branchVector.g2[num]; - double b2 = branchVector.b2[num]; if (variable.equals(v2Var)) { return di2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v2(), ph2()); } else if (variable.equals(ph2Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java index 06bf1ebf3f..e6799e3d78 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide1ReactiveFlowEquationTerm.java @@ -8,6 +8,9 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; +import net.jafama.FastMath; import java.util.Objects; @@ -20,10 +23,14 @@ public class OpenBranchSide1ReactiveFlowEquationTerm extends AbstractOpenSide1Br private final Variable v2Var; - public OpenBranchSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); - v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + public OpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus2, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2.getNum(), AcVariableType.BUS_V); + } + + private double v2() { + return sv.get(v2Var.getRow()); } public static double q2(double y, double cosKsi, double sinKsi, double g1, double b1, double b2, double v2) { @@ -38,14 +45,14 @@ public static double dq2dv2(double y, double cosKsi, double sinKsi, double g1, d @Override public double eval() { - return branchVector.q2[num]; + return q2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, b2, v2()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v2Var)) { - return branchVector.dq2dv2[num]; + return dq2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, b2, v2()); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java index 4ecffcaf90..84160f3130 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java @@ -8,6 +8,9 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; +import net.jafama.FastMath; import java.util.Objects; @@ -18,10 +21,18 @@ public class OpenBranchSide2ActiveFlowEquationTerm extends AbstractOpenSide2Bran private final Variable v1Var; - public OpenBranchSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); - v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + public OpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus1, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); + } + + private double v1() { + return sv.get(v1Var.getRow()); + } + + private double r1() { + return element.getPiModel().getR1(); } public static double p1(double y, double cosKsi, double sinKsi, double g1, double g2, double b2, double v1, double r1) { @@ -36,14 +47,14 @@ public static double dp1dv1(double y, double cosKsi, double sinKsi, double g1, d @Override public double eval() { - return branchVector.p1[num]; + return p1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, g2, b2, v1(), r1()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return branchVector.dp1dv1[num]; + return dp1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, g2, b2, v1(), r1()); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java index 367fe38030..35ebb08a93 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; import net.jafama.FastMath; import java.util.Objects; @@ -24,13 +26,13 @@ public class OpenBranchSide2CurrentMagnitudeEquationTerm extends AbstractOpenSid private Variable r1Var; - public OpenBranchSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); - v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); - ph1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_PHI); + public OpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus1, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); + ph1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_PHI); if (deriveR1) { - r1Var = variableSet.getVariable(bus1Num, AcVariableType.BRANCH_RHO1); + r1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BRANCH_RHO1); } } @@ -43,7 +45,7 @@ private double ph1() { } private double r1() { - return r1Var != null ? sv.get(r1Var.getRow()) : branchVector.r1[num]; + return r1Var != null ? sv.get(r1Var.getRow()) : element.getPiModel().getR1(); } private static double gres(double y, double sinksi, double g1, double g2, double b2, double shunt) { @@ -78,25 +80,19 @@ private static double dimI1dv1(double y, double cosKsi, double sinKsi, double g1 return r1 * r1 * (gres(y, sinKsi, g1, g2, b2, shunt) * FastMath.sin(ph1) + bres(y, cosKsi, b1, g2, b2, shunt) * FastMath.cos(ph1)); } - private static double di1dv1(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double b2, double v1, double ph1, double r1) { + public static double di1dv1(double y, double cosKsi, double sinKsi, double g1, double b1, double g2, double b2, double v1, double ph1, double r1) { return (reI1(y, cosKsi, sinKsi, g1, b1, g2, b2, v1, ph1, r1) * dreI1dv1(y, cosKsi, sinKsi, g1, b1, g2, b2, ph1, r1) + imI1(y, cosKsi, sinKsi, g1, b1, g2, b2, v1, ph1, r1) * dimI1dv1(y, cosKsi, sinKsi, g1, b1, g2, b2, ph1, r1)) / i1(y, cosKsi, sinKsi, g1, b1, g2, b2, v1, ph1, r1); } @Override public double eval() { - return branchVector.i1[num]; + return i1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v1(), ph1(), r1()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); - double y = branchVector.y[num]; - double ksi = branchVector.ksi[num]; - double g1 = branchVector.g1[num]; - double b1 = branchVector.b1[num]; - double b2 = branchVector.b2[num]; - double g2 = branchVector.g2[num]; if (variable.equals(v1Var)) { return di1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v1(), ph1(), r1()); } else if (variable.equals(ph1Var) || variable.equals(r1Var)) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java index f5282cd875..817bd2372c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java @@ -8,6 +8,9 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; +import net.jafama.FastMath; import java.util.Objects; @@ -18,10 +21,18 @@ public class OpenBranchSide2ReactiveFlowEquationTerm extends AbstractOpenSide2Br private final Variable v1Var; - public OpenBranchSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); - v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + public OpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, VariableSet variableSet, + boolean deriveA1, boolean deriveR1) { + super(branch, AcVariableType.BUS_V, bus1, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V); + } + + private double v1() { + return sv.get(v1Var.getRow()); + } + + private double r1() { + return element.getPiModel().getR1(); } public static double q1(double y, double cosKsi, double sinKsi, double b1, double g2, double b2, double v1, double r1) { @@ -36,14 +47,14 @@ public static double dq1dv1(double y, double cosKsi, double sinKsi, double b1, d @Override public double eval() { - return branchVector.q1[num]; + return q1(y, FastMath.cos(ksi), FastMath.sin(ksi), b1, g2, b2, v1(), r1()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(v1Var)) { - return branchVector.dq1dv1[num]; + return dq1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), b1, g2, b2, v1(), r1()); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java index 913e80c55f..39d5b8705e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorActiveFlowEquationTerm.java @@ -8,6 +8,8 @@ import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBus; +import com.powsybl.openloadflow.network.LfShunt; import java.util.List; import java.util.Objects; @@ -19,8 +21,8 @@ public class ShuntCompensatorActiveFlowEquationTerm extends AbstractShuntCompens private final List> variables; - public ShuntCompensatorActiveFlowEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet) { - super(shuntVector, num, busNum, variableSet); + public ShuntCompensatorActiveFlowEquationTerm(LfShunt shunt, LfBus bus, VariableSet variableSet) { + super(shunt, bus, variableSet); variables = List.of(vVar); } @@ -29,6 +31,10 @@ public List> getVariables() { return variables; } + private double g() { + return element.getG(); + } + public static double p(double v, double g) { return g * v * v; } @@ -39,14 +45,14 @@ public static double dpdv(double v, double g) { @Override public double eval() { - return shuntVector.p[num]; + return p(v(), g()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(vVar)) { - return shuntVector.dpdv[num]; + return dpdv(v(), g()); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java index 23c5a652b3..0d72cef279 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java @@ -9,6 +9,8 @@ import com.powsybl.math.matrix.DenseMatrix; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.LfBus; +import com.powsybl.openloadflow.network.LfShunt; import java.util.List; import java.util.Objects; @@ -22,10 +24,10 @@ public class ShuntCompensatorReactiveFlowEquationTerm extends AbstractShuntCompe private final List> variables; - public ShuntCompensatorReactiveFlowEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet, boolean deriveB) { - super(shuntVector, num, busNum, variableSet); + public ShuntCompensatorReactiveFlowEquationTerm(LfShunt shunt, LfBus bus, VariableSet variableSet, boolean deriveB) { + super(shunt, bus, variableSet); if (deriveB) { - bVar = variableSet.getVariable(num, AcVariableType.SHUNT_B); + bVar = variableSet.getVariable(shunt.getNum(), AcVariableType.SHUNT_B); variables = List.of(vVar, bVar); } else { variables = List.of(vVar); @@ -38,7 +40,7 @@ public List> getVariables() { } private double b() { - return bVar != null ? sv.get(bVar.getRow()) : shuntVector.b[num]; + return bVar != null ? sv.get(bVar.getRow()) : element.getB(); } public static double q(double v, double b) { @@ -55,16 +57,16 @@ public static double dqdb(double v) { @Override public double eval() { - return shuntVector.q[num]; + return q(v(), b()); } @Override public double der(Variable variable) { Objects.requireNonNull(variable); if (variable.equals(vVar)) { - return shuntVector.dqdv[num]; + return dqdv(v(), b()); } else if (variable.equals(bVar)) { - return shuntVector.dqdb[num]; + return dqdb(v()); } else { throw new IllegalStateException("Unknown variable: " + variable); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index 0899543111..8c6371aa07 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -101,7 +101,6 @@ protected void createBusEquation(LfBus bus, AcEquationSystemCreationContext crea @Override protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); - var branchVector = creationContext.getNetworkVector().getBranchVector(); // positive sequence EquationTerm p1 = null; @@ -130,24 +129,24 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE if (!branch.isAsymmetric()) { // no asymmetry is detected with this line, we handle the equations as decoupled // positive - p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); - i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); - i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1); + p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + q1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + p2 = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + q2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.POSITIVE); + i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); // zero - ixz1 = new ClosedBranchI1xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); - iyz1 = new ClosedBranchI1yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); - ixz2 = new ClosedBranchI2xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); - iyz2 = new ClosedBranchI2yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + ixz1 = new ClosedBranchI1xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + iyz1 = new ClosedBranchI1yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + ixz2 = new ClosedBranchI2xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); + iyz2 = new ClosedBranchI2yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.ZERO); // negative - ixn1 = new ClosedBranchI1xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); - iyn1 = new ClosedBranchI1yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); - ixn2 = new ClosedBranchI2xFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); - iyn2 = new ClosedBranchI2yFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + ixn1 = new ClosedBranchI1xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + iyn1 = new ClosedBranchI1yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + ixn2 = new ClosedBranchI2xFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); + iyn2 = new ClosedBranchI2yFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1, SequenceType.NEGATIVE); } else { // assymmetry is detected with this line, we handle the equations as coupled between the different sequences // positive diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractBranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractBranchVectorAcFlowEquationTerm.java new file mode 100644 index 0000000000..aa374be2b2 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractBranchVectorAcFlowEquationTerm.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2021, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcEquationType; +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.AbstractNamedEquationTerm; +import com.powsybl.openloadflow.network.ElementType; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public abstract class AbstractBranchVectorAcFlowEquationTerm extends AbstractNamedEquationTerm { + + protected final AcBranchVector branchVector; + + protected final int num; + + protected AbstractBranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int num) { + super(!Objects.requireNonNull(branchVector).disabled[num]); + this.branchVector = Objects.requireNonNull(branchVector); + this.num = num; + } + + @Override + public ElementType getElementType() { + return ElementType.BRANCH; + } + + @Override + public int getElementNum() { + return num; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java new file mode 100644 index 0000000000..b4d8ed0ab5 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java @@ -0,0 +1,135 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.math.matrix.DenseMatrix; +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static com.powsybl.openloadflow.network.PiModel.A2; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public abstract class AbstractClosedBranchVectorAcFlowEquationTerm extends AbstractBranchVectorAcFlowEquationTerm { + + protected final Variable v1Var; + + protected final Variable v2Var; + + protected final Variable ph1Var; + + protected final Variable ph2Var; + + protected final Variable a1Var; + + protected final Variable r1Var; + + protected final List> variables = new ArrayList<>(); + + private static AcVariableType getVoltageMagnitudeType(Fortescue.SequenceType sequenceType) { + return switch (sequenceType) { + case POSITIVE -> AcVariableType.BUS_V; + case NEGATIVE -> AcVariableType.BUS_V_NEGATIVE; + case ZERO -> AcVariableType.BUS_V_ZERO; + }; + } + + private static AcVariableType getVoltageAngleType(Fortescue.SequenceType sequenceType) { + return switch (sequenceType) { + case POSITIVE -> AcVariableType.BUS_PHI; + case NEGATIVE -> AcVariableType.BUS_PHI_NEGATIVE; + case ZERO -> AcVariableType.BUS_PHI_ZERO; + }; + } + + protected AbstractClosedBranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum); + Objects.requireNonNull(variableSet); + AcVariableType vType = getVoltageMagnitudeType(sequenceType); + AcVariableType angleType = getVoltageAngleType(sequenceType); + v1Var = variableSet.getVariable(bus1Num, vType); + v2Var = variableSet.getVariable(bus2Num, vType); + ph1Var = variableSet.getVariable(bus1Num, angleType); + ph2Var = variableSet.getVariable(bus2Num, angleType); + a1Var = deriveA1 ? variableSet.getVariable(branchNum, AcVariableType.BRANCH_ALPHA1) : null; + r1Var = deriveR1 ? variableSet.getVariable(branchNum, AcVariableType.BRANCH_RHO1) : null; + variables.add(v1Var); + variables.add(v2Var); + variables.add(ph1Var); + variables.add(ph2Var); + if (a1Var != null) { + variables.add(a1Var); + } + if (r1Var != null) { + variables.add(r1Var); + } + } + + public Variable getA1Var() { + return a1Var; + } + + protected double v1() { + return sv.get(v1Var.getRow()); + } + + protected double v2() { + return sv.get(v2Var.getRow()); + } + + protected double ph1() { + return sv.get(ph1Var.getRow()); + } + + protected double ph2() { + return sv.get(ph2Var.getRow()); + } + + protected double r1() { + return r1Var != null ? sv.get(r1Var.getRow()) : branchVector.r1[num]; + } + + protected double a1() { + return a1Var != null ? sv.get(a1Var.getRow()) : branchVector.a1[num]; + } + + public static double theta1(double ksi, double ph1, double a1, double ph2) { + return ksi - a1 + A2 - ph1 + ph2; + } + + public static double theta2(double ksi, double ph1, double a1, double ph2) { + return ksi + a1 - A2 + ph1 - ph2; + } + + protected abstract double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1); + + @Override + public double calculateSensi(DenseMatrix dx, int column) { + Objects.requireNonNull(dx); + double dph1 = dx.get(ph1Var.getRow(), column); + double dph2 = dx.get(ph2Var.getRow(), column); + double dv1 = dx.get(v1Var.getRow(), column); + double dv2 = dx.get(v2Var.getRow(), column); + double da1 = a1Var != null ? dx.get(a1Var.getRow(), column) : 0; + double dr1 = r1Var != null ? dx.get(r1Var.getRow(), column) : 0; + return calculateSensi(dph1, dph2, dv1, dv2, da1, dr1); + } + + @Override + public List> getVariables() { + return variables; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java new file mode 100644 index 0000000000..96f2138671 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.List; + +/** + * @author Gael Macherel {@literal } + */ +abstract class AbstractOpenSide1BranchVectorAcFlowEquationTerm extends AbstractBranchVectorAcFlowEquationTerm { + + protected final List> variables; + + protected AbstractOpenSide1BranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, + AcVariableType variableType, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum); + variables = List.of(variableSet.getVariable(bus2Num, variableType)); + if (deriveA1 || deriveR1) { + throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); + } + } + + protected static double shunt(double y, double cosKsi, double sinKsi, double g1, double b1) { + return (g1 + y * sinKsi) * (g1 + y * sinKsi) + (-b1 + y * cosKsi) * (-b1 + y * cosKsi); + } + + @Override + public List> getVariables() { + return variables; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java new file mode 100644 index 0000000000..51ea888527 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.List; + +/** + * @author Gael Macherel {@literal } + */ +abstract class AbstractOpenSide2BranchVectorAcFlowEquationTerm extends AbstractBranchVectorAcFlowEquationTerm { + + protected final List> variables; + + protected AbstractOpenSide2BranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, + AcVariableType variableType, int bus1Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum); + variables = List.of(variableSet.getVariable(bus1Num, variableType)); + if (deriveA1 || deriveR1) { + throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); + } + } + + protected static double shunt(double y, double cosKsi, double sinKsi, double g2, double b2) { + return (g2 + y * sinKsi) * (g2 + y * sinKsi) + (-b2 + y * cosKsi) * (-b2 + y * cosKsi); + } + + @Override + public List> getVariables() { + return variables; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractShuntVectorCompensatorEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractShuntVectorCompensatorEquationTerm.java new file mode 100644 index 0000000000..69123f8b53 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractShuntVectorCompensatorEquationTerm.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2022, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcEquationType; +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.AbstractNamedEquationTerm; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.network.ElementType; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public abstract class AbstractShuntVectorCompensatorEquationTerm extends AbstractNamedEquationTerm { + + protected final AcShuntVector shuntVector; + + protected final int num; + + protected final Variable vVar; + + protected AbstractShuntVectorCompensatorEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet) { + super(!Objects.requireNonNull(shuntVector).disabled[num]); + this.shuntVector = Objects.requireNonNull(shuntVector); + this.num = num; + Objects.requireNonNull(variableSet); + vVar = variableSet.getVariable(busNum, AcVariableType.BUS_V); + } + + @Override + public ElementType getElementType() { + return ElementType.SHUNT_COMPENSATOR; + } + + @Override + public int getElementNum() { + return num; + } + + protected double v() { + return sv.get(vVar.getRow()); + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java similarity index 96% rename from src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java rename to src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java index 20a5036c9c..b56c63e3a6 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java @@ -4,8 +4,10 @@ * 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.ac.equations; +package com.powsybl.openloadflow.ac.equations.vector; +import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreationParameters; +import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreator; import com.powsybl.openloadflow.network.LfBranch; import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.network.PiModel; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBusVector.java similarity index 93% rename from src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java rename to src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBusVector.java index d632554f68..02486f8573 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcBusVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBusVector.java @@ -4,7 +4,7 @@ * 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.ac.equations; +package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.network.LfBus; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java similarity index 98% rename from src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java rename to src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java index 07ec3dbaa0..d21eb49240 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java @@ -4,9 +4,10 @@ * 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.ac.equations; +package com.powsybl.openloadflow.ac.equations.vector; import com.google.common.base.Stopwatch; +import com.powsybl.openloadflow.ac.equations.*; import com.powsybl.openloadflow.equations.*; import com.powsybl.openloadflow.network.*; import net.jafama.DoubleWrapper; @@ -154,12 +155,12 @@ public void updatePowerFlows() { double a1 = branchVector.a1Row[branchNum] != -1 ? state[branchVector.a1Row[branchNum]] : branchVector.a1[branchNum]; - double theta1 = AbstractClosedBranchAcFlowEquationTerm.theta1( + double theta1 = AbstractClosedBranchVectorAcFlowEquationTerm.theta1( branchVector.ksi[branchNum], ph1, a1, ph2); - double theta2 = AbstractClosedBranchAcFlowEquationTerm.theta2( + double theta2 = AbstractClosedBranchVectorAcFlowEquationTerm.theta2( branchVector.ksi[branchNum], ph1, a1, diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcShuntVector.java similarity index 96% rename from src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java rename to src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcShuntVector.java index 8895355dcc..c9a0f4f62b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcShuntVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcShuntVector.java @@ -4,7 +4,7 @@ * 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.ac.equations; +package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.network.LfBus; import com.powsybl.openloadflow.network.LfShunt; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreationContext.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreationContext.java new file mode 100644 index 0000000000..dfdf85ec8c --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreationContext.java @@ -0,0 +1,31 @@ +/** + * 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreationContext; +import com.powsybl.openloadflow.ac.equations.AcEquationType; +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.EquationSystem; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian + */ +public class AcVectorizedEquationSystemCreationContext extends AcEquationSystemCreationContext { + + private final AcNetworkVector networkVector; + + public AcVectorizedEquationSystemCreationContext(EquationSystem equationSystem, AcNetworkVector networkVector) { + super(equationSystem); + this.networkVector = Objects.requireNonNull(networkVector); + } + + public AcNetworkVector getNetworkVector() { + return networkVector; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java new file mode 100644 index 0000000000..e2c22f01cc --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java @@ -0,0 +1,113 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.*; +import com.powsybl.openloadflow.equations.EquationSystem; +import com.powsybl.openloadflow.equations.EquationTerm; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.LfBus; +import com.powsybl.openloadflow.network.LfNetwork; +import com.powsybl.openloadflow.network.LfShunt; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class AcVectorizedEquationSystemCreator extends AcEquationSystemCreator { + + public AcVectorizedEquationSystemCreator(LfNetwork network, AcEquationSystemCreationParameters creationParameters) { + super(network, creationParameters); + } + + private AcShuntVector getShuntVector(AcEquationSystemCreationContext creationContext) { + return ((AcVectorizedEquationSystemCreationContext) creationContext).getNetworkVector().getShuntVector(); + } + + private AcBranchVector getBranchVector(AcEquationSystemCreationContext creationContext) { + return ((AcVectorizedEquationSystemCreationContext) creationContext).getNetworkVector().getBranchVector(); + } + + @Override + protected EquationTerm createShuntCompensatorActiveFlowEquationTerm(LfShunt shunt, LfBus bus, AcEquationSystemCreationContext creationContext) { + return new ShuntVectorCompensatorActiveFlowEquationTerm(getShuntVector(creationContext), shunt.getNum(), shunt.getNum(), creationContext.getEquationSystem().getVariableSet()); + } + + @Override + protected EquationTerm createShuntCompensatorReactiveFlowEquationTerm(LfShunt shunt, LfBus bus, boolean deriveB, AcEquationSystemCreationContext creationContext) { + return new ShuntVectorCompensatorReactiveFlowEquationTerm(getShuntVector(creationContext), shunt.getNum(), shunt.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveB); + } + + @Override + protected EquationTerm createClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchVectorSide1ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchVectorSide1ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchVectorSide1CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchVectorSide2ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchVectorSide2ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new ClosedBranchVectorSide2CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createOpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide2ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createOpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide2ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createOpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide2CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createOpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide1ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createOpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide1ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + protected EquationTerm createOpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide1CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + } + + @Override + public EquationSystem create() { + EquationSystem equationSystem = new EquationSystem<>(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + AcVectorizedEquationSystemCreationContext creationContext = new AcVectorizedEquationSystemCreationContext(equationSystem, networkVector); + create(creationContext); + networkVector.startListening(); + return equationSystem; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java new file mode 100644 index 0000000000..b074cdc40d --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; +import net.jafama.FastMath; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide1ActiveFlowEquationTerm.*; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +@SuppressWarnings("squid:S00107") +public class ClosedBranchVectorSide1ActiveFlowEquationTerm extends AbstractClosedBranchVectorAcFlowEquationTerm { + + public ClosedBranchVectorSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + } + + public ClosedBranchVectorSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + } + + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double v1 = v1(); + double r1 = r1(); + double v2 = v2(); + double theta = theta1(ksi, ph1(), a1(), ph2()); + double cosTheta = FastMath.cos(theta); + double sinTheta = FastMath.sin(theta); + double sinKsi = FastMath.sin(ksi); + return dp1dph1(y, v1, r1, v2, cosTheta) * dph1 + + dp1dph2(y, v1, r1, v2, cosTheta) * dph2 + + dp1dv1(y, sinKsi, g1, v1, r1, v2, sinTheta) * dv1 + + dp1dv2(y, v1, r1, sinTheta) * dv2 + + dp1da1(y, v1, r1, v2, cosTheta) * da1 + + dp1dr1(y, sinKsi, g1, v1, r1, v2, sinTheta) * dr1; + } + + @Override + public double eval() { + return branchVector.p1[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v1Var)) { + return branchVector.dp1dv1[num]; + } else if (variable.equals(v2Var)) { + return branchVector.dp1dv2[num]; + } else if (variable.equals(ph1Var)) { + return branchVector.dp1dph1[num]; + } else if (variable.equals(ph2Var)) { + return branchVector.dp1dph2[num]; + } else if (variable.equals(a1Var)) { + return branchVector.dp1da1[num]; + } else if (variable.equals(r1Var)) { + return branchVector.dp1dr1[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_p_closed_1"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java new file mode 100644 index 0000000000..36bb7e0e31 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide1CurrentMagnitudeEquationTerm.*; + +/** + * @author Gael Macherel {@literal } + */ +@SuppressWarnings("squid:S00107") +public class ClosedBranchVectorSide1CurrentMagnitudeEquationTerm extends AbstractClosedBranchVectorAcFlowEquationTerm { + + public ClosedBranchVectorSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + } + + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + if (dr1 != 0) { + throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); + } + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; + double v1 = v1(); + double ph1 = ph1(); + double r1 = r1(); + double a1 = a1(); + double v2 = v2(); + double ph2 = ph2(); + return di1dph1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dph1 + + di1dph2(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dph2 + + di1dv1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dv1 + + di1dv2(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dv2 + + di1da1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * da1; + } + + @Override + public double eval() { + return branchVector.i1[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; + if (variable.equals(v1Var)) { + return di1dv1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(v2Var)) { + return di1dv2(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(ph1Var)) { + return di1dph1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(ph2Var)) { + return di1dph2(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(a1Var)) { + return di1da1(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(r1Var)) { + throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_i_closed_1"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java new file mode 100644 index 0000000000..dd87c45ea5 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; +import net.jafama.FastMath; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide1ReactiveFlowEquationTerm.*; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +@SuppressWarnings("squid:S00107") +public class ClosedBranchVectorSide1ReactiveFlowEquationTerm extends AbstractClosedBranchVectorAcFlowEquationTerm { + + public ClosedBranchVectorSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + } + + public ClosedBranchVectorSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + } + + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double b1 = branchVector.b1[num]; + double v1 = v1(); + double r1 = r1(); + double v2 = v2(); + double theta = theta1(ksi, ph1(), a1(), ph2()); + double cosTheta = FastMath.cos(theta); + double sinTheta = FastMath.sin(theta); + double cosKsi = FastMath.cos(ksi); + return dq1dph1(y, v1, r1, v2, sinTheta) * dph1 + + dq1dph2(y, v1, r1, v2, sinTheta) * dph2 + + dq1dv1(y, cosKsi, b1, v1, r1, v2, cosTheta) * dv1 + + dq1dv2(y, v1, r1, cosTheta) * dv2 + + dq1da1(y, v1, r1, v2, sinTheta) * da1 + + dq1dr1(y, cosKsi, b1, v1, r1, v2, cosTheta) * dr1; + } + + @Override + public double eval() { + return branchVector.q1[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v1Var)) { + return branchVector.dq1dv1[num]; + } else if (variable.equals(v2Var)) { + return branchVector.dq1dv2[num]; + } else if (variable.equals(ph1Var)) { + return branchVector.dq1dph1[num]; + } else if (variable.equals(ph2Var)) { + return branchVector.dq1dph2[num]; + } else if (variable.equals(a1Var)) { + return branchVector.dq1da1[num]; + } else if (variable.equals(r1Var)) { + return branchVector.dq1dr1[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_q_closed_1"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java new file mode 100644 index 0000000000..d470025537 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; +import net.jafama.FastMath; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide2ActiveFlowEquationTerm.*; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +@SuppressWarnings("squid:S00107") +public class ClosedBranchVectorSide2ActiveFlowEquationTerm extends AbstractClosedBranchVectorAcFlowEquationTerm { + + public ClosedBranchVectorSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + } + + public ClosedBranchVectorSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + } + + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g2 = branchVector.g2[num]; + double v1 = v1(); + double r1 = r1(); + double v2 = v2(); + double theta = theta2(ksi, ph1(), a1(), ph2()); + double cosTheta = FastMath.cos(theta); + double sinTheta = FastMath.sin(theta); + return dp2dph1(y, v1, r1, v2, cosTheta) * dph1 + + dp2dph2(y, v1, r1, v2, cosTheta) * dph2 + + dp2dv1(y, r1, v2, sinTheta) * dv1 + + dp2dv2(y, FastMath.sin(ksi), g2, v1, r1, v2, sinTheta) * dv2 + + dp2da1(y, v1, r1, v2, cosTheta) * da1 + + dp2dr1(y, v1, v2, sinTheta) * dr1; + } + + @Override + public double eval() { + return branchVector.p2[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v1Var)) { + return branchVector.dp2dv1[num]; + } else if (variable.equals(v2Var)) { + return branchVector.dp2dv2[num]; + } else if (variable.equals(ph1Var)) { + return branchVector.dp2dph1[num]; + } else if (variable.equals(ph2Var)) { + return branchVector.dp2dph2[num]; + } else if (variable.equals(a1Var)) { + return branchVector.dp2da1[num]; + } else if (variable.equals(r1Var)) { + return branchVector.dp2dr1[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_p_closed_2"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java new file mode 100644 index 0000000000..6f4d26ba64 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide2CurrentMagnitudeEquationTerm.*; + +/** + * @author Gael Macherel {@literal } + */ +@SuppressWarnings("squid:S00107") +public class ClosedBranchVectorSide2CurrentMagnitudeEquationTerm extends AbstractClosedBranchVectorAcFlowEquationTerm { + + public ClosedBranchVectorSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + } + + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + if (dr1 != 0) { + throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); + } + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g2 = branchVector.g2[num]; + double b2 = branchVector.b2[num]; + double v1 = v1(); + double ph1 = ph1(); + double r1 = r1(); + double a1 = a1(); + double v2 = v2(); + double ph2 = ph2(); + return di2dph1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dph1 + + di2dph2(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dph2 + + di2dv1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dv1 + + di2dv2(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dv2 + + di2da1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * da1; + } + + @Override + public double eval() { + return branchVector.i2[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g2 = branchVector.g2[num]; + double b2 = branchVector.b2[num]; + if (variable.equals(v1Var)) { + return di2dv1(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(v2Var)) { + return di2dv2(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(ph1Var)) { + return di2dph1(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(ph2Var)) { + return di2dph2(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(a1Var)) { + return di2da1(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2()); + } else if (variable.equals(r1Var)) { + throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_i_closed_2"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java new file mode 100644 index 0000000000..a892405ab4 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import com.powsybl.openloadflow.util.Fortescue; +import net.jafama.FastMath; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide2ReactiveFlowEquationTerm.*; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +@SuppressWarnings("squid:S00107") +public class ClosedBranchVectorSide2ReactiveFlowEquationTerm extends AbstractClosedBranchVectorAcFlowEquationTerm { + + public ClosedBranchVectorSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); + } + + public ClosedBranchVectorSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1, + Fortescue.SequenceType sequenceType) { + super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); + } + + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double b2 = branchVector.b2[num]; + double v1 = v1(); + double r1 = r1(); + double v2 = v2(); + double theta = theta2(ksi, ph1(), a1(), ph2()); + double cosTheta = FastMath.cos(theta); + double sinTheta = FastMath.sin(theta); + return dq2dph1(y, v1, r1, v2, sinTheta) * dph1 + + dq2dph2(y, v1, r1, v2, sinTheta) * dph2 + + dq2dv1(y, r1, v2, cosTheta) * dv1 + + dq2dv2(y, FastMath.cos(ksi), b2, v1, r1, v2, cosTheta) * dv2 + + dq2da1(y, v1, r1, v2, sinTheta) * da1 + + dq2dr1(y, v1, v2, cosTheta) * dr1; + } + + @Override + public double eval() { + return branchVector.q2[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v1Var)) { + return branchVector.dq2dv1[num]; + } else if (variable.equals(v2Var)) { + return branchVector.dq2dv2[num]; + } else if (variable.equals(ph1Var)) { + return branchVector.dq2dph1[num]; + } else if (variable.equals(ph2Var)) { + return branchVector.dq2dph2[num]; + } else if (variable.equals(a1Var)) { + return branchVector.dq2da1[num]; + } else if (variable.equals(r1Var)) { + return branchVector.dq2dr1[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_q_closed_2"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java new file mode 100644 index 0000000000..b3859947a9 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class OpenBranchVectorSide1ActiveFlowEquationTerm extends AbstractOpenSide1BranchVectorAcFlowEquationTerm { + + private final Variable v2Var; + + public OpenBranchVectorSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + } + + @Override + public double eval() { + return branchVector.p2[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v2Var)) { + return branchVector.dp2dv2[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_p_open_1"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java new file mode 100644 index 0000000000..9dd6c90d2d --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import net.jafama.FastMath; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.OpenBranchSide1CurrentMagnitudeEquationTerm.di2dv2; + +/** + * @author Gael Macherel {@literal } + */ +@SuppressWarnings("squid:S00107") +public class OpenBranchVectorSide1CurrentMagnitudeEquationTerm extends AbstractOpenSide1BranchVectorAcFlowEquationTerm { + + private final Variable v2Var; + + private final Variable ph2Var; + + public OpenBranchVectorSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + ph2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_PHI); + } + + private double v2() { + return sv.get(v2Var.getRow()); + } + + private double ph2() { + return sv.get(ph2Var.getRow()); + } + + @Override + public double eval() { + return branchVector.i2[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; + double g2 = branchVector.g2[num]; + double b2 = branchVector.b2[num]; + if (variable.equals(v2Var)) { + return di2dv2(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v2(), ph2()); + } else if (variable.equals(ph2Var)) { + throw new IllegalArgumentException("Derivative with respect to ph2 not implemented"); + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_i_open_1"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java new file mode 100644 index 0000000000..4e9a973517 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class OpenBranchVectorSide1ReactiveFlowEquationTerm extends AbstractOpenSide1BranchVectorAcFlowEquationTerm { + + private final Variable v2Var; + + public OpenBranchVectorSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); + } + + @Override + public double eval() { + return branchVector.q2[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v2Var)) { + return branchVector.dq2dv2[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_q_open_1"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java new file mode 100644 index 0000000000..3d977833fd --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class OpenBranchVectorSide2ActiveFlowEquationTerm extends AbstractOpenSide2BranchVectorAcFlowEquationTerm { + + private final Variable v1Var; + + public OpenBranchVectorSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + } + + @Override + public double eval() { + return branchVector.p1[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v1Var)) { + return branchVector.dp1dv1[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_p_open_2"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java new file mode 100644 index 0000000000..5ff165726b --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; +import net.jafama.FastMath; + +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.OpenBranchSide2CurrentMagnitudeEquationTerm.di1dv1; + +/** + * @author Gael Macherel {@literal } + */ +@SuppressWarnings("squid:S00107") +public class OpenBranchVectorSide2CurrentMagnitudeEquationTerm extends AbstractOpenSide2BranchVectorAcFlowEquationTerm { + + private final Variable v1Var; + + private final Variable ph1Var; + + private Variable r1Var; + + public OpenBranchVectorSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + ph1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_PHI); + if (deriveR1) { + r1Var = variableSet.getVariable(bus1Num, AcVariableType.BRANCH_RHO1); + } + } + + private double v1() { + return sv.get(v1Var.getRow()); + } + + private double ph1() { + return sv.get(ph1Var.getRow()); + } + + private double r1() { + return r1Var != null ? sv.get(r1Var.getRow()) : branchVector.r1[num]; + } + + @Override + public double eval() { + return branchVector.i1[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + double y = branchVector.y[num]; + double ksi = branchVector.ksi[num]; + double g1 = branchVector.g1[num]; + double b1 = branchVector.b1[num]; + double b2 = branchVector.b2[num]; + double g2 = branchVector.g2[num]; + if (variable.equals(v1Var)) { + return di1dv1(y, FastMath.cos(ksi), FastMath.sin(ksi), g1, b1, g2, b2, v1(), ph1(), r1()); + } else if (variable.equals(ph1Var) || variable.equals(r1Var)) { + throw new IllegalArgumentException("Derivative with respect to ph1 or r1 not implemented"); + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_i_open_2"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java new file mode 100644 index 0000000000..d53bcf1bab --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class OpenBranchVectorSide2ReactiveFlowEquationTerm extends AbstractOpenSide2BranchVectorAcFlowEquationTerm { + + private final Variable v1Var; + + public OpenBranchVectorSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, + VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); + } + + @Override + public double eval() { + return branchVector.q1[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(v1Var)) { + return branchVector.dq1dv1[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_q_open_2"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorActiveFlowEquationTerm.java new file mode 100644 index 0000000000..a0291c8c0d --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorActiveFlowEquationTerm.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.List; +import java.util.Objects; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class ShuntVectorCompensatorActiveFlowEquationTerm extends AbstractShuntVectorCompensatorEquationTerm { + + private final List> variables; + + public ShuntVectorCompensatorActiveFlowEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet) { + super(shuntVector, num, busNum, variableSet); + variables = List.of(vVar); + } + + @Override + public List> getVariables() { + return variables; + } + + @Override + public double eval() { + return shuntVector.p[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(vVar)) { + return shuntVector.dpdv[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + protected String getName() { + return "ac_p_shunt"; + } +} diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java new file mode 100644 index 0000000000..801cde5279 --- /dev/null +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2019, 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.ac.equations.vector; + +import com.powsybl.math.matrix.DenseMatrix; +import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.equations.Variable; +import com.powsybl.openloadflow.equations.VariableSet; + +import java.util.List; +import java.util.Objects; + +import static com.powsybl.openloadflow.ac.equations.ShuntCompensatorReactiveFlowEquationTerm.*; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +public class ShuntVectorCompensatorReactiveFlowEquationTerm extends AbstractShuntVectorCompensatorEquationTerm { + + private Variable bVar; + + private final List> variables; + + public ShuntVectorCompensatorReactiveFlowEquationTerm(AcShuntVector shuntVector, int num, int busNum, VariableSet variableSet, boolean deriveB) { + super(shuntVector, num, busNum, variableSet); + if (deriveB) { + bVar = variableSet.getVariable(num, AcVariableType.SHUNT_B); + variables = List.of(vVar, bVar); + } else { + variables = List.of(vVar); + } + } + + @Override + public List> getVariables() { + return variables; + } + + private double b() { + return bVar != null ? sv.get(bVar.getRow()) : shuntVector.b[num]; + } + + @Override + public double eval() { + return shuntVector.q[num]; + } + + @Override + public double der(Variable variable) { + Objects.requireNonNull(variable); + if (variable.equals(vVar)) { + return shuntVector.dqdv[num]; + } else if (variable.equals(bVar)) { + return shuntVector.dqdb[num]; + } else { + throw new IllegalStateException("Unknown variable: " + variable); + } + } + + @Override + public double calculateSensi(DenseMatrix dx, int column) { + double dv = dx.get(vVar.getRow(), column); + double db = bVar != null ? dx.get(bVar.getRow(), column) : 0; + double v = v(); + double b = b(); + return dqdv(v, b) * dv + dqdb(v) * db; + } + + @Override + protected String getName() { + return "ac_q_shunt"; + } +} diff --git a/src/test/java/com/powsybl/openloadflow/EquationsTest.java b/src/test/java/com/powsybl/openloadflow/EquationsTest.java index ecd6ec0dbd..2f2b232797 100644 --- a/src/test/java/com/powsybl/openloadflow/EquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/EquationsTest.java @@ -56,7 +56,7 @@ public Object answer(InvocationOnMock invocation) { private static final double A_1 = 0.324294234; private static final double V_2 = 1.0718794209362505; private static final double PH_2 = 0.18609589391040748; - private static final double B_SHUNT = 0.2748383993949494; + private static final double B = 0.2748383993949494; private static final double DROOP = 103.13240312354819; private static final double P_0 = 1.9280906677246095; private static final double LOSS_FACTOR_1 = 0.01100000023841858; @@ -95,8 +95,6 @@ private static & Quantity, E extends Enum & Quantity> doub return values; } - private LfNetwork network; - private LfBranch branch; private LfBus bus1; @@ -113,48 +111,17 @@ void setUp() { Mockito.doReturn(R).when(piModel).getR(); Mockito.doReturn(X).when(piModel).getX(); Mockito.doReturn(Y).when(piModel).getY(); - Mockito.doReturn(1 / Y).when(piModel).getZ(); Mockito.doReturn(G_1).when(piModel).getG1(); Mockito.doReturn(G_2).when(piModel).getG2(); Mockito.doReturn(B_1).when(piModel).getB1(); Mockito.doReturn(B_2).when(piModel).getB2(); Mockito.doReturn(KSI).when(piModel).getKsi(); Mockito.doReturn(R_1).when(piModel).getR1(); - Mockito.doReturn(A_1).when(piModel).getA1(); bus1 = Mockito.mock(LfBus.class, ANSWER); bus2 = Mockito.mock(LfBus.class, ANSWER); Mockito.doReturn(0).when(bus1).getNum(); Mockito.doReturn(1).when(bus2).getNum(); - - network = Mockito.mock(LfNetwork.class); - Mockito.doReturn(List.of(bus1, bus2)).when(network).getBuses(); - Mockito.doReturn(List.of(branch)).when(network).getBranches(); - } - - private AcBranchVector createBranchVector(LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, - EquationSystem equationSystem, - Variable v1Var, Variable v2Var, - Variable ph1Var, Variable ph2Var, - Variable a1Var, Variable r1Var) { - Mockito.doReturn(deriveA1).when(branch).isPhaseController(); - Mockito.doReturn(deriveR1).when(branch).isVoltageController(); - Mockito.doReturn(bus1).when(branch).getBus1(); - Mockito.doReturn(bus2).when(branch).getBus2(); - - AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); - AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); - AcBusVector busVector = networkVector.getBusVector(); - AcBranchVector branchVector = networkVector.getBranchVector(); - busVector.vRow[0] = v1Var.getRow(); - busVector.vRow[1] = v2Var.getRow(); - busVector.phRow[0] = ph1Var.getRow(); - busVector.phRow[1] = ph2Var.getRow(); - branchVector.a1Row[0] = a1Var.getRow(); - branchVector.r1Row[0] = r1Var.getRow(); - networkVector.copyVariablesToBranches(); - networkVector.updatePowerFlows(); - return networkVector.getBranchVector(); } @Test @@ -177,62 +144,55 @@ void branchTest() { a1Var.setRow(5); unknownVar.setRow(6); - EquationSystem equationSystem = new EquationSystem<>(); - var sv = equationSystem.getStateVector(); - sv.set(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); + var sv = new StateVector(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); // closed branch equations - AcBranchVector branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {41.78173051479356, 48.66261692116701, 138.21343172859858, 29.31710523088579, -138.21343172859858, 54.62161149356045, 138.21343172859858, Double.NaN, 270.81476537421185}, - eval(new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-3.500079625302254, 122.46444997806617, 31.42440177840898, -128.9449438332101, -31.42440177840898, 137.46086897280827, 31.42440177840898, Double.NaN, 162.40477689607334}, - eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); - assertArrayEquals(new double[] {39.13246485286219, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, - eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {39.13246485286217, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, + eval(new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {-40.6365773800554, -48.52391742324069, -131.8614376204652, -27.319027760225953, 131.8614376204652, -54.4659275092331, -131.8614376204652, Double.NaN, -262.1703103131649}, - eval(new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); assertArrayEquals(new double[] {16.04980301110306, -123.06939783256767, 51.99045110393844, 152.96594042215764, -51.99045110393844, -138.1398958886022, 51.99045110393844, Double.NaN, -56.2529021950738}, - eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); - assertArrayEquals(new double[] {40.76137216481359, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, - eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + eval(new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {40.7613721648136, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, + eval(new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true), variables, sv)); // open branch equations - branchVector = createBranchVector(null, bus2, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {0.1717595025847833, Double.NaN, Double.NaN, 0.3204828812456483, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {0.3752024940555977, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); - - branchVector = createBranchVector(bus1, null, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); - assertArrayEquals(new double[] {0.15639470221220209, Double.NaN, Double.NaN, 0.2919337476186018, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {-0.33122369717493005, Double.NaN, Double.NaN, -0.6182778179094991, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); - assertArrayEquals(new double[] {0.34186721585930596, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); - - branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); + eval(new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {0.37520249405559764, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {0.15652310047954035, Double.NaN, Double.NaN, 0.2920535601715773, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchSide2ActiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {-0.331495628053771, Double.NaN, Double.NaN, -0.6185315653587614, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {0.3420075216110214, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus2, variableSet, false, false), variables, sv)); + // assert current equation is consistent with active and reactive power ones - var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + var p1Eq = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); p1Eq.setStateVector(sv); double p1 = p1Eq.eval(); - var q1Eq = new ClosedBranchSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + var q1Eq = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); q1Eq.setStateVector(sv); double q1 = q1Eq.eval(); - var i1Eq = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + var i1Eq = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true); i1Eq.setStateVector(sv); double i1 = i1Eq.eval(); assertEquals(i1, Math.hypot(p1, q1) / V_1, 10e-14); - var p2Eq = new ClosedBranchSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + var p2Eq = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); p2Eq.setStateVector(sv); double p2 = p2Eq.eval(); - var q2Eq = new ClosedBranchSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + var q2Eq = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, variableSet, true, true); q2Eq.setStateVector(sv); double q2 = q2Eq.eval(); - var i2Eq = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + var i2Eq = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, variableSet, true, true); i2Eq.setStateVector(sv); double i2 = i2Eq.eval(); assertEquals(i2, Math.hypot(p2, q2) / V_2, 10e-14); @@ -266,17 +226,10 @@ void shuntTest() { var shunt = Mockito.mock(LfShunt.class, new RuntimeExceptionAnswer()); Mockito.doReturn(0).when(shunt).getNum(); Mockito.doReturn(G_SHUNT).when(shunt).getG(); - Mockito.doReturn(B_SHUNT).when(shunt).getB(); - Mockito.doReturn(true).when(shunt).hasVoltageControlCapability(); Mockito.doReturn(false).when(shunt).isDisabled(); var bus = Mockito.mock(LfBus.class, ANSWER); Mockito.doReturn(0).when(bus).getNum(); - Mockito.doReturn(bus).when(shunt).getBus(); - - LfNetwork network = Mockito.mock(LfNetwork.class); - Mockito.doReturn(List.of(shunt)).when(network).getShunts(); - Mockito.doReturn(List.of(bus)).when(network).getBuses(); VariableSet variableSet = new VariableSet<>(); var vVar = variableSet.getVariable(0, AcVariableType.BUS_V); @@ -288,19 +241,12 @@ void shuntTest() { bVar.setRow(1); unknownVar.setRow(2); - EquationSystem equationSystem = new EquationSystem<>(); - var sv = equationSystem.getStateVector(); - sv.set(new double[] {V_1, B_SHUNT, 0}); + var sv = new StateVector(new double[] {V_1, B, 0}); - AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); - AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); - networkVector.getBusVector().vRow[bus.getNum()] = vVar.getRow(); - networkVector.getShuntVector().bRow[shunt.getNum()] = bVar.getRow(); - networkVector.updatePowerFlows(); assertArrayEquals(new double[] {4.275919696380507E-5, 7.98163392892194E-5, Double.NaN, Double.NaN, Double.NaN}, - eval(new ShuntCompensatorActiveFlowEquationTerm(networkVector.getShuntVector(), shunt.getNum(), bus.getNum(), variableSet), variables, sv)); + eval(new ShuntCompensatorActiveFlowEquationTerm(shunt, bus, variableSet), variables, sv)); assertArrayEquals(new double[] {-0.3155098135679268, -0.588945539602459, -1.1479830120627779, Double.NaN, -1.7369285516652369}, - eval(new ShuntCompensatorReactiveFlowEquationTerm(networkVector.getShuntVector(), shunt.getNum(), bus.getNum(), variableSet, true), variables, sv)); + eval(new ShuntCompensatorReactiveFlowEquationTerm(shunt, bus, variableSet, true), variables, sv)); } @Test diff --git a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java new file mode 100644 index 0000000000..0eac50b8f5 --- /dev/null +++ b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java @@ -0,0 +1,343 @@ +/** + * Copyright (c) 2022, 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; + +import com.powsybl.commons.PowsyblException; +import com.powsybl.math.matrix.DenseMatrix; +import com.powsybl.openloadflow.ac.equations.*; +import com.powsybl.openloadflow.ac.equations.vector.*; +import com.powsybl.openloadflow.dc.equations.ClosedBranchSide1DcFlowEquationTerm; +import com.powsybl.openloadflow.dc.equations.ClosedBranchSide2DcFlowEquationTerm; +import com.powsybl.openloadflow.dc.equations.DcApproximationType; +import com.powsybl.openloadflow.dc.equations.DcVariableType; +import com.powsybl.openloadflow.equations.*; +import com.powsybl.openloadflow.network.*; +import com.powsybl.openloadflow.network.impl.LfVscConverterStationImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Geoffroy Jamgotchian {@literal } + */ +class VectorEquationsTest { + + public static class RuntimeExceptionAnswer implements Answer { + + public Object answer(InvocationOnMock invocation) { + throw new PowsyblException(invocation.getMethod().getName() + " is not stubbed"); + } + } + + private static final RuntimeExceptionAnswer ANSWER = new RuntimeExceptionAnswer(); + + private static final double R = 5.872576933488291E-4; + private static final double X = 0.007711911135433123; + private static final double Y = 129.29521139058275; + private static final double KSI = 0.07600275710144264; + private static final double G_1 = 0.08448324029284184; + private static final double G_2 = 0.06483244848429284; + private static final double B_1 = 0.13324220618233085; + private static final double B_2 = 0.18320177723653615; + private static final double V_1 = 1.0714396912858781; + private static final double PH_1 = 0.1613653508202422; + private static final double R_1 = 0.95455; + private static final double A_1 = 0.324294234; + private static final double V_2 = 1.0718794209362505; + private static final double PH_2 = 0.18609589391040748; + private static final double B_SHUNT = 0.2748383993949494; + private static final double DROOP = 103.13240312354819; + private static final double P_0 = 1.9280906677246095; + private static final double LOSS_FACTOR_1 = 0.01100000023841858; + private static final double LOSS_FACTOR_2 = 0.02400453453002384; + private static final double G_SHUNT = 0.0000372472384299244; + + private static & Quantity, E extends Enum & Quantity> double[] eval(EquationTerm term, List> variables, StateVector sv) { + term.setStateVector(sv); + double[] values = new double[variables.size() + 2]; + + // equation value + values[0] = term.eval(); + + // all derivative values + for (int i = 0; i < variables.size(); i++) { + var v = variables.get(i); + try { + values[i + 1] = term.der(v); + } catch (IllegalArgumentException | IllegalStateException e) { + // not supported or no implemented + values[i + 1] = Double.NaN; + } + } + + // sensitivity value + double[] one = new double[values.length]; + Arrays.fill(one, 1); + DenseMatrix dx = new DenseMatrix(values.length, 1, one); + try { + values[values.length - 1] = term.calculateSensi(dx, 0); + } catch (UnsupportedOperationException | IllegalArgumentException e) { + // not supported + values[values.length - 1] = Double.NaN; + } + + return values; + } + + private LfNetwork network; + + private LfBranch branch; + + private LfBus bus1; + + private LfBus bus2; + + @BeforeEach + void setUp() { + branch = Mockito.mock(LfBranch.class, ANSWER); + Mockito.doReturn(0).when(branch).getNum(); + Mockito.doReturn(false).when(branch).isDisabled(); + PiModel piModel = Mockito.mock(PiModel.class, ANSWER); + Mockito.doReturn(piModel).when(branch).getPiModel(); + Mockito.doReturn(R).when(piModel).getR(); + Mockito.doReturn(X).when(piModel).getX(); + Mockito.doReturn(Y).when(piModel).getY(); + Mockito.doReturn(1 / Y).when(piModel).getZ(); + Mockito.doReturn(G_1).when(piModel).getG1(); + Mockito.doReturn(G_2).when(piModel).getG2(); + Mockito.doReturn(B_1).when(piModel).getB1(); + Mockito.doReturn(B_2).when(piModel).getB2(); + Mockito.doReturn(KSI).when(piModel).getKsi(); + Mockito.doReturn(R_1).when(piModel).getR1(); + Mockito.doReturn(A_1).when(piModel).getA1(); + + bus1 = Mockito.mock(LfBus.class, ANSWER); + bus2 = Mockito.mock(LfBus.class, ANSWER); + Mockito.doReturn(0).when(bus1).getNum(); + Mockito.doReturn(1).when(bus2).getNum(); + + network = Mockito.mock(LfNetwork.class); + Mockito.doReturn(List.of(bus1, bus2)).when(network).getBuses(); + Mockito.doReturn(List.of(branch)).when(network).getBranches(); + } + + private AcBranchVector createBranchVector(LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, + EquationSystem equationSystem, + Variable v1Var, Variable v2Var, + Variable ph1Var, Variable ph2Var, + Variable a1Var, Variable r1Var) { + Mockito.doReturn(deriveA1).when(branch).isPhaseController(); + Mockito.doReturn(deriveR1).when(branch).isVoltageController(); + Mockito.doReturn(bus1).when(branch).getBus1(); + Mockito.doReturn(bus2).when(branch).getBus2(); + + AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + AcBusVector busVector = networkVector.getBusVector(); + AcBranchVector branchVector = networkVector.getBranchVector(); + busVector.vRow[0] = v1Var.getRow(); + busVector.vRow[1] = v2Var.getRow(); + busVector.phRow[0] = ph1Var.getRow(); + busVector.phRow[1] = ph2Var.getRow(); + branchVector.a1Row[0] = a1Var.getRow(); + branchVector.r1Row[0] = r1Var.getRow(); + networkVector.copyVariablesToBranches(); + networkVector.updatePowerFlows(); + return networkVector.getBranchVector(); + } + + @Test + void branchTest() { + VariableSet variableSet = new VariableSet<>(); + var v1Var = variableSet.getVariable(0, AcVariableType.BUS_V); + var ph1Var = variableSet.getVariable(0, AcVariableType.BUS_PHI); + var v2Var = variableSet.getVariable(1, AcVariableType.BUS_V); + var ph2Var = variableSet.getVariable(1, AcVariableType.BUS_PHI); + var r1Var = variableSet.getVariable(0, AcVariableType.BRANCH_RHO1); + var a1Var = variableSet.getVariable(0, AcVariableType.BRANCH_ALPHA1); + var unknownVar = variableSet.getVariable(999, AcVariableType.BUS_V); + + var variables = List.of(v1Var, ph1Var, v2Var, ph2Var, r1Var, a1Var, unknownVar); + v1Var.setRow(0); + ph1Var.setRow(1); + v2Var.setRow(2); + ph2Var.setRow(3); + r1Var.setRow(4); + a1Var.setRow(5); + unknownVar.setRow(6); + + EquationSystem equationSystem = new EquationSystem<>(); + var sv = equationSystem.getStateVector(); + sv.set(new double[] {V_1, PH_1, V_2, PH_2, R_1, A_1, 0}); + + // closed branch equations + AcBranchVector branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); + assertArrayEquals(new double[] {41.78173051479356, 48.66261692116701, 138.21343172859858, 29.31710523088579, -138.21343172859858, 54.62161149356045, 138.21343172859858, Double.NaN, 270.81476537421185}, + eval(new ClosedBranchVectorSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {-3.500079625302254, 122.46444997806617, 31.42440177840898, -128.9449438332101, -31.42440177840898, 137.46086897280827, 31.42440177840898, Double.NaN, 162.40477689607334}, + eval(new ClosedBranchVectorSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {39.13246485286219, -0.8052805161189096, 126.09926753871545, 37.31322159867258, -126.09926753871542, Double.NaN, 126.09926753871542, Double.NaN, Double.NaN}, + eval(new ClosedBranchVectorSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {-40.6365773800554, -48.52391742324069, -131.8614376204652, -27.319027760225953, 131.8614376204652, -54.4659275092331, -131.8614376204652, Double.NaN, -262.1703103131649}, + eval(new ClosedBranchVectorSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {16.04980301110306, -123.06939783256767, 51.99045110393844, 152.96594042215764, -51.99045110393844, -138.1398958886022, 51.99045110393844, Double.NaN, -56.2529021950738}, + eval(new ClosedBranchVectorSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + assertArrayEquals(new double[] {40.76137216481359, -0.07246503940372644, 132.23571821183896, 38.10038077658943, -132.23571821183896, Double.NaN, 132.23571821183896, Double.NaN, Double.NaN}, + eval(new ClosedBranchVectorSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true), variables, sv)); + + // open branch equations + branchVector = createBranchVector(null, bus2, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); + assertArrayEquals(new double[] {0.1717595025847833, Double.NaN, Double.NaN, 0.3204828812456483, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchVectorSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchVectorSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {0.3752024940555977, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchVectorSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + + branchVector = createBranchVector(bus1, null, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); + assertArrayEquals(new double[] {0.15639470221220209, Double.NaN, Double.NaN, 0.2919337476186018, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchVectorSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {-0.33122369717493005, Double.NaN, Double.NaN, -0.6182778179094991, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchVectorSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + assertArrayEquals(new double[] {0.34186721585930596, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, + eval(new OpenBranchVectorSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + + branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); + // assert current equation is consistent with active and reactive power ones + var p1Eq = new ClosedBranchVectorSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + p1Eq.setStateVector(sv); + double p1 = p1Eq.eval(); + var q1Eq = new ClosedBranchVectorSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + q1Eq.setStateVector(sv); + double q1 = q1Eq.eval(); + var i1Eq = new ClosedBranchVectorSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + i1Eq.setStateVector(sv); + double i1 = i1Eq.eval(); + assertEquals(i1, Math.hypot(p1, q1) / V_1, 10e-14); + + var p2Eq = new ClosedBranchVectorSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + p2Eq.setStateVector(sv); + double p2 = p2Eq.eval(); + var q2Eq = new ClosedBranchVectorSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + q2Eq.setStateVector(sv); + double q2 = q2Eq.eval(); + var i2Eq = new ClosedBranchVectorSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus1.getNum(), bus2.getNum(), variableSet, true, true); + i2Eq.setStateVector(sv); + double i2 = i2Eq.eval(); + assertEquals(i2, Math.hypot(p2, q2) / V_2, 10e-14); + } + + @Test + void dcBranchTest() { + VariableSet variableSet = new VariableSet<>(); + var ph1Var = variableSet.getVariable(0, DcVariableType.BUS_PHI); + var ph2Var = variableSet.getVariable(1, DcVariableType.BUS_PHI); + var a1Var = variableSet.getVariable(0, DcVariableType.BRANCH_ALPHA1); + var unknownVar = variableSet.getVariable(999, DcVariableType.DUMMY_P); + + var variables = List.of(ph1Var, ph2Var, a1Var, unknownVar); + ph1Var.setRow(0); + ph2Var.setRow(1); + a1Var.setRow(2); + unknownVar.setRow(3); + + var sv = new StateVector(new double[]{PH_1, PH_2, A_1, 0}); + + // closed branch equations + assertArrayEquals(new double[] {37.07881433490131, 123.77606318805043, -123.77606318805043, 123.77606318805043, Double.NaN, 123.77606318805043}, + eval(ClosedBranchSide1DcFlowEquationTerm.create(branch, bus1, bus2, variableSet, true, true, DcApproximationType.IGNORE_R), variables, sv)); + assertArrayEquals(new double[] {-37.07881433490131, -123.77606318805043, 123.77606318805043, -123.77606318805043, Double.NaN, -123.77606318805043}, + eval(ClosedBranchSide2DcFlowEquationTerm.create(branch, bus1, bus2, variableSet, true, true, DcApproximationType.IGNORE_R), variables, sv)); + } + + @Test + void shuntTest() { + var shunt = Mockito.mock(LfShunt.class, new RuntimeExceptionAnswer()); + Mockito.doReturn(0).when(shunt).getNum(); + Mockito.doReturn(G_SHUNT).when(shunt).getG(); + Mockito.doReturn(B_SHUNT).when(shunt).getB(); + Mockito.doReturn(true).when(shunt).hasVoltageControlCapability(); + Mockito.doReturn(false).when(shunt).isDisabled(); + + var bus = Mockito.mock(LfBus.class, ANSWER); + Mockito.doReturn(0).when(bus).getNum(); + Mockito.doReturn(bus).when(shunt).getBus(); + + LfNetwork network = Mockito.mock(LfNetwork.class); + Mockito.doReturn(List.of(shunt)).when(network).getShunts(); + Mockito.doReturn(List.of(bus)).when(network).getBuses(); + + VariableSet variableSet = new VariableSet<>(); + var vVar = variableSet.getVariable(0, AcVariableType.BUS_V); + var bVar = variableSet.getVariable(0, AcVariableType.SHUNT_B); + var unknownVar = variableSet.getVariable(999, AcVariableType.BUS_V); + + var variables = List.of(vVar, bVar, unknownVar); + vVar.setRow(0); + bVar.setRow(1); + unknownVar.setRow(2); + + EquationSystem equationSystem = new EquationSystem<>(); + var sv = equationSystem.getStateVector(); + sv.set(new double[] {V_1, B_SHUNT, 0}); + + AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); + AcNetworkVector networkVector = new AcNetworkVector(network, equationSystem, creationParameters); + networkVector.getBusVector().vRow[bus.getNum()] = vVar.getRow(); + networkVector.getShuntVector().bRow[shunt.getNum()] = bVar.getRow(); + networkVector.updatePowerFlows(); + assertArrayEquals(new double[] {4.275919696380507E-5, 7.98163392892194E-5, Double.NaN, Double.NaN, Double.NaN}, + eval(new ShuntVectorCompensatorActiveFlowEquationTerm(networkVector.getShuntVector(), shunt.getNum(), bus.getNum(), variableSet), variables, sv)); + assertArrayEquals(new double[] {-0.3155098135679268, -0.588945539602459, -1.1479830120627779, Double.NaN, -1.7369285516652369}, + eval(new ShuntVectorCompensatorReactiveFlowEquationTerm(networkVector.getShuntVector(), shunt.getNum(), bus.getNum(), variableSet, true), variables, sv)); + } + + @Test + void hvdcTest() { + var hvdc = Mockito.mock(LfHvdc.class, new RuntimeExceptionAnswer()); + Mockito.doReturn(0).when(hvdc).getNum(); + Mockito.doReturn(false).when(hvdc).isDisabled(); + Mockito.doReturn(DROOP).when(hvdc).getDroop(); + Mockito.doReturn(P_0).when(hvdc).getP0(); + LfVscConverterStationImpl station1 = Mockito.mock(LfVscConverterStationImpl.class, new RuntimeExceptionAnswer()); + LfVscConverterStationImpl station2 = Mockito.mock(LfVscConverterStationImpl.class, new RuntimeExceptionAnswer()); + Mockito.doReturn(station1).when(hvdc).getConverterStation1(); + Mockito.doReturn(station2).when(hvdc).getConverterStation2(); + Mockito.doReturn(LOSS_FACTOR_1).when(station1).getLossFactor(); + Mockito.doReturn(LOSS_FACTOR_2).when(station2).getLossFactor(); + + var bus1 = Mockito.mock(LfBus.class, ANSWER); + var bus2 = Mockito.mock(LfBus.class, ANSWER); + Mockito.doReturn(0).when(bus1).getNum(); + Mockito.doReturn(1).when(bus2).getNum(); + + VariableSet variableSet = new VariableSet<>(); + var hvdcPh1Var = variableSet.getVariable(0, AcVariableType.BUS_PHI); + var hvdcPh2Var = variableSet.getVariable(1, AcVariableType.BUS_PHI); + var unknownVar = variableSet.getVariable(999, AcVariableType.BUS_V); + + var variables = List.of(hvdcPh1Var, hvdcPh2Var, unknownVar); + hvdcPh1Var.setRow(0); + hvdcPh2Var.setRow(1); + unknownVar.setRow(2); + + var sv = new StateVector(new double[] {PH_1, PH_2, 0}); + + assertArrayEquals(new double[] {-144.1554855266458, 5906.983150087268, -5906.983150087268, Double.NaN, Double.NaN}, + eval(new HvdcAcEmulationSide1ActiveFlowEquationTerm(hvdc, bus1, bus2, variableSet), variables, sv)); + assertArrayEquals(new double[] {144.20596034441598, -5909.051430021139, 5909.051430021139, Double.NaN, Double.NaN}, + eval(new HvdcAcEmulationSide2ActiveFlowEquationTerm(hvdc, bus1, bus2, variableSet), variables, sv)); + } +} diff --git a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java index 7fb573dacb..b0c4a59f2d 100644 --- a/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java +++ b/src/test/java/com/powsybl/openloadflow/network/impl/LfSwitchTest.java @@ -14,7 +14,6 @@ import com.powsybl.openloadflow.OpenLoadFlowParameters; import com.powsybl.openloadflow.ac.equations.*; import com.powsybl.openloadflow.ac.AcLoadFlowParameters; -import com.powsybl.openloadflow.equations.EquationSystem; import com.powsybl.openloadflow.equations.EquationTerm; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.graph.EvenShiloachGraphDecrementalConnectivityFactory; @@ -70,19 +69,16 @@ void getterTest() { void setterTest() { lfSwitch.getPiModel().setX(LfNetworkParameters.LOW_IMPEDANCE_THRESHOLD_DEFAULT_VALUE); - EquationSystem equationSystem = new EquationSystem<>(); - AcEquationSystemCreationParameters creationParameters = new AcEquationSystemCreationParameters(); - AcNetworkVector networkVector = new AcNetworkVector(lfNetwork, equationSystem, creationParameters); VariableSet variableSet = new VariableSet<>(); - EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); - EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); + EquationTerm p1 = new ClosedBranchSide1ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); + EquationTerm p2 = new ClosedBranchSide2ActiveFlowEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); lfSwitch.setP1(p1); assertEquals(Double.NaN, lfSwitch.getP1().eval()); lfSwitch.setP2(p2); assertEquals(Double.NaN, lfSwitch.getP2().eval()); - EquationTerm i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); - EquationTerm i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(networkVector.getBranchVector(), lfSwitch.getNum(), lfSwitch.getBus1().getNum(), lfSwitch.getBus2().getNum(), variableSet, false, false); + EquationTerm i1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); + EquationTerm i2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(lfSwitch, lfSwitch.getBus1(), lfSwitch.getBus2(), variableSet, false, false); lfSwitch.setI1(i1); assertEquals(Double.NaN, lfSwitch.getP1().eval()); lfSwitch.setI2(i2); From 8344582a950a4fb9cff28be544cff61ba4b99bf7 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sun, 3 Dec 2023 14:10:39 +0100 Subject: [PATCH 23/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/ac/AcLoadFlowContext.java | 6 +++--- .../ac/equations/AcEquationSystemCreator.java | 2 +- .../ac/equations/AcEquationSystemUpdater.java | 7 +++++-- .../AcVectorizedEquationSystemCreator.java | 4 ++-- .../ac/outerloop/PhaseControlOuterLoop.java | 19 ++++++++++++------- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java b/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java index abf99f932b..4e0dde3366 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java +++ b/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java @@ -6,15 +6,15 @@ */ package com.powsybl.openloadflow.ac; -import com.powsybl.openloadflow.ac.equations.asym.AsymmetricalAcEquationSystemCreator; -import com.powsybl.openloadflow.equations.JacobianMatrix; -import com.powsybl.openloadflow.lf.AbstractLoadFlowContext; import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreator; import com.powsybl.openloadflow.ac.equations.AcEquationType; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.asym.AsymmetricalAcEquationSystemCreator; import com.powsybl.openloadflow.equations.EquationSystem; import com.powsybl.openloadflow.equations.EquationVector; +import com.powsybl.openloadflow.equations.JacobianMatrix; import com.powsybl.openloadflow.equations.TargetVector; +import com.powsybl.openloadflow.lf.AbstractLoadFlowContext; import com.powsybl.openloadflow.network.LfNetwork; /** diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 1b422eedfb..5714d90cf6 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -859,7 +859,7 @@ protected void create(AcEquationSystemCreationContext creationContext) { EquationSystemPostProcessor.findAll().forEach(pp -> pp.onCreate(equationSystem)); - network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(equationSystem, this))); + network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(equationSystem, this, creationContext))); } public EquationSystem create() { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java index 43cb41ec05..f1cf8bf575 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java @@ -20,10 +20,13 @@ public class AcEquationSystemUpdater extends AbstractEquationSystemUpdater equationSystem, - AcEquationSystemCreator creator) { + AcEquationSystemCreator creator, AcEquationSystemCreationContext creationContext) { super(equationSystem, LoadFlowModel.AC); this.creator = Objects.requireNonNull(creator); + this.creationContext = Objects.requireNonNull(creationContext); } private void updateVoltageControls(LfBus bus) { @@ -127,7 +130,7 @@ private void recreateDistributionEquations(LfZeroImpedanceNetwork network) { for (LfBus bus : network.getGraph().vertexSet()) { bus.getGeneratorVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> creator.recreateReactivePowerDistributionEquations(voltageControl, new AcEquationSystemCreationContext(equationSystem))); + .ifPresent(voltageControl -> creator.recreateReactivePowerDistributionEquations(voltageControl, creationContext)); bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, equationSystem)); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java index e2c22f01cc..eb7988e82e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java @@ -33,12 +33,12 @@ private AcBranchVector getBranchVector(AcEquationSystemCreationContext creationC @Override protected EquationTerm createShuntCompensatorActiveFlowEquationTerm(LfShunt shunt, LfBus bus, AcEquationSystemCreationContext creationContext) { - return new ShuntVectorCompensatorActiveFlowEquationTerm(getShuntVector(creationContext), shunt.getNum(), shunt.getNum(), creationContext.getEquationSystem().getVariableSet()); + return new ShuntVectorCompensatorActiveFlowEquationTerm(getShuntVector(creationContext), shunt.getNum(), bus.getNum(), creationContext.getEquationSystem().getVariableSet()); } @Override protected EquationTerm createShuntCompensatorReactiveFlowEquationTerm(LfShunt shunt, LfBus bus, boolean deriveB, AcEquationSystemCreationContext creationContext) { - return new ShuntVectorCompensatorReactiveFlowEquationTerm(getShuntVector(creationContext), shunt.getNum(), shunt.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveB); + return new ShuntVectorCompensatorReactiveFlowEquationTerm(getShuntVector(creationContext), shunt.getNum(), bus.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveB); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/outerloop/PhaseControlOuterLoop.java b/src/main/java/com/powsybl/openloadflow/ac/outerloop/PhaseControlOuterLoop.java index 32bb223639..a8366b4b39 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/outerloop/PhaseControlOuterLoop.java +++ b/src/main/java/com/powsybl/openloadflow/ac/outerloop/PhaseControlOuterLoop.java @@ -13,11 +13,13 @@ import com.powsybl.openloadflow.ac.AcOuterLoopContext; import com.powsybl.openloadflow.ac.equations.AcEquationType; import com.powsybl.openloadflow.ac.equations.AcVariableType; -import com.powsybl.openloadflow.ac.equations.ClosedBranchSide1CurrentMagnitudeEquationTerm; -import com.powsybl.openloadflow.ac.equations.ClosedBranchSide2CurrentMagnitudeEquationTerm; +import com.powsybl.openloadflow.equations.EquationTerm; import com.powsybl.openloadflow.lf.outerloop.AbstractPhaseControlOuterLoop; import com.powsybl.openloadflow.lf.outerloop.OuterLoopStatus; -import com.powsybl.openloadflow.network.*; +import com.powsybl.openloadflow.network.Direction; +import com.powsybl.openloadflow.network.LfBranch; +import com.powsybl.openloadflow.network.PiModel; +import com.powsybl.openloadflow.network.TransformerPhaseControl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -119,13 +121,16 @@ private boolean changeTapPositions(TransformerPhaseControl phaseControl) { return false; } + @SuppressWarnings("unchecked") private boolean isSensitivityCurrentPerA1Positive(LfBranch controllerBranch, TwoSides controlledSide) { if (controlledSide == TwoSides.ONE) { - ClosedBranchSide1CurrentMagnitudeEquationTerm i1 = (ClosedBranchSide1CurrentMagnitudeEquationTerm) controllerBranch.getI1(); - return i1.der(i1.getA1Var()) > 0; + EquationTerm i1 = (EquationTerm) controllerBranch.getI1(); + var a1Var = i1.getVariables().stream().filter(v -> v.getType() == AcVariableType.BRANCH_ALPHA1).findFirst().orElseThrow(); + return i1.der(a1Var) > 0; } else { - ClosedBranchSide2CurrentMagnitudeEquationTerm i2 = (ClosedBranchSide2CurrentMagnitudeEquationTerm) controllerBranch.getI2(); - return i2.der(i2.getA1Var()) > 0; + EquationTerm i2 = (EquationTerm) controllerBranch.getI2(); + var a1Var = i2.getVariables().stream().filter(v -> v.getType() == AcVariableType.BRANCH_ALPHA1).findFirst().orElseThrow(); + return i2.der(a1Var) > 0; } } } From 6ed47b6ea3674561bd15edcd73e982d3d5060d4a Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Mon, 4 Dec 2023 20:26:33 +0100 Subject: [PATCH 24/32] Wip Signed-off-by: Geoffroy Jamgotchian --- ...osedBranchSide1ActiveFlowEquationTerm.java | 13 ++++++---- ...anchVectorSide1ActiveFlowEquationTerm.java | 25 +++---------------- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java index d7e08ca128..5d847fa7e5 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ActiveFlowEquationTerm.java @@ -33,11 +33,10 @@ public ClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBu super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta1(ksi, ph1(), a1(), ph2()); + public static double calculateSensi(double g1, double y, double ksi, + double v1, double ph1, double a1, double r1, double v2, double ph2, + double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double theta = theta1(ksi, ph1, a1, ph2); double cosTheta = FastMath.cos(theta); double sinTheta = FastMath.sin(theta); double sinKsi = FastMath.sin(ksi); @@ -49,6 +48,10 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 + dp1dr1(y, sinKsi, g1, v1, r1, v2, sinTheta) * dr1; } + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + return calculateSensi(g1, y, ksi, v1(), ph1(), a1(), r1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); + } + public static double p1(double y, double sinKsi, double g1, double v1, double r1, double v2, double sinTheta) { return r1 * v1 * (g1 * r1 * v1 + y * r1 * v1 * sinKsi - y * R2 * v2 * sinTheta); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java index b074cdc40d..af877f0e6d 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ActiveFlowEquationTerm.java @@ -7,15 +7,13 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ClosedBranchSide1ActiveFlowEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.util.Fortescue; -import net.jafama.FastMath; import java.util.Objects; -import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide1ActiveFlowEquationTerm.*; - /** * @author Geoffroy Jamgotchian {@literal } */ @@ -27,29 +25,12 @@ public ClosedBranchVectorSide1ActiveFlowEquationTerm(AcBranchVector branchVector super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchVectorSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); - } - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; double g1 = branchVector.g1[num]; - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta1(ksi, ph1(), a1(), ph2()); - double cosTheta = FastMath.cos(theta); - double sinTheta = FastMath.sin(theta); - double sinKsi = FastMath.sin(ksi); - return dp1dph1(y, v1, r1, v2, cosTheta) * dph1 - + dp1dph2(y, v1, r1, v2, cosTheta) * dph2 - + dp1dv1(y, sinKsi, g1, v1, r1, v2, sinTheta) * dv1 - + dp1dv2(y, v1, r1, sinTheta) * dv2 - + dp1da1(y, v1, r1, v2, cosTheta) * da1 - + dp1dr1(y, sinKsi, g1, v1, r1, v2, sinTheta) * dr1; + return ClosedBranchSide1ActiveFlowEquationTerm.calculateSensi(g1, y, ksi, v1(), ph1(), a1(), r1(), v2(), ph2(), + dph1, dph2, dv1, dv2, da1, dr1); } @Override From 88f6e7ab9d9fa57c63e0d1dc8d0aa4d30e2c0ea5 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 5 Dec 2023 22:01:46 +0100 Subject: [PATCH 25/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcEquationSystemCreator.java | 84 ++++++++++--------- .../AsymmetricalAcEquationSystemCreator.java | 2 +- ...enSide1BranchVectorAcFlowEquationTerm.java | 5 +- ...enSide2BranchVectorAcFlowEquationTerm.java | 5 +- .../AcVectorizedEquationSystemCreator.java | 24 +++--- ...anchVectorSide1ActiveFlowEquationTerm.java | 4 +- ...ctorSide1CurrentMagnitudeEquationTerm.java | 4 +- ...chVectorSide1ReactiveFlowEquationTerm.java | 4 +- ...anchVectorSide2ActiveFlowEquationTerm.java | 4 +- ...ctorSide2CurrentMagnitudeEquationTerm.java | 4 +- ...chVectorSide2ReactiveFlowEquationTerm.java | 4 +- .../openloadflow/VectorEquationsTest.java | 12 +-- 12 files changed, 79 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index a07d09d069..2afe229c6e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -8,13 +8,19 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.iidm.network.TwoSides; -import com.powsybl.openloadflow.equations.*; +import com.powsybl.openloadflow.equations.Equation; +import com.powsybl.openloadflow.equations.EquationSystem; +import com.powsybl.openloadflow.equations.EquationSystemPostProcessor; +import com.powsybl.openloadflow.equations.EquationTerm; import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.network.TransformerPhaseControl.Mode; import com.powsybl.openloadflow.util.Evaluable; import com.powsybl.openloadflow.util.EvaluableConstants; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -636,7 +642,7 @@ public static boolean isDeriveR1(LfBranch branch) { } protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, - EquationSystem equationSystem) { + AcEquationSystemCreationContext creationContext) { // effective equations, could be closed one or open one Evaluable p1 = null; Evaluable q1 = null; @@ -664,21 +670,21 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1 = isDeriveA1(branch, creationParameters); boolean deriveR1 = isDeriveR1(branch); if (bus1 != null && bus2 != null) { - closedP1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - closedQ1 = new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - closedP2 = new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - closedQ2 = new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - closedI1 = new ClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); - closedI2 = new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1); + closedP1 = createClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + closedQ1 = createClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + closedP2 = createClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + closedQ2 = createClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + closedI1 = createClosedBranchSide1CurrentMagnitudeEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); + closedI2 = createClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, deriveA1, deriveR1, creationContext); if (branch.isDisconnectionAllowedSide1()) { - openP2 = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, equationSystem.getVariableSet()); - openQ2 = new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, equationSystem.getVariableSet()); - openI2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, equationSystem.getVariableSet()); + openP2 = createOpenBranchSide1ActiveFlowEquationTerm(branch, bus2, creationContext); + openQ2 = createOpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, creationContext); + openI2 = createOpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, creationContext); } if (branch.isDisconnectionAllowedSide2()) { - openP1 = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, equationSystem.getVariableSet()); - openQ1 = new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, equationSystem.getVariableSet()); - openI1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, equationSystem.getVariableSet(), deriveR1); + openP1 = createOpenBranchSide2ActiveFlowEquationTerm(branch, bus1, creationContext); + openQ1 = createOpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, creationContext); + openI1 = createOpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, deriveR1, creationContext); } p1 = closedP1; q1 = closedQ1; @@ -687,9 +693,9 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, q2 = closedQ2; i2 = closedI2; } else if (bus1 != null) { - openP1 = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, equationSystem.getVariableSet()); - openQ1 = new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, equationSystem.getVariableSet()); - openI1 = new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, equationSystem.getVariableSet(), deriveR1); + openP1 = createOpenBranchSide2ActiveFlowEquationTerm(branch, bus1, creationContext); + openQ1 = createOpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, creationContext); + openI1 = createOpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, deriveR1, creationContext); p1 = openP1; q1 = openQ1; i1 = openI1; @@ -697,9 +703,9 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, q2 = EvaluableConstants.ZERO; i2 = EvaluableConstants.ZERO; } else if (bus2 != null) { - openP2 = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, equationSystem.getVariableSet()); - openQ2 = new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, equationSystem.getVariableSet()); - openI2 = new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, equationSystem.getVariableSet()); + openP2 = createOpenBranchSide1ActiveFlowEquationTerm(branch, bus2, creationContext); + openQ2 = createOpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, creationContext); + openI2 = createOpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, creationContext); p1 = EvaluableConstants.ZERO; q1 = EvaluableConstants.ZERO; i1 = EvaluableConstants.ZERO; @@ -708,7 +714,7 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, i2 = openI2; } - createImpedantBranchEquations(branch, bus1, bus2, equationSystem, + createImpedantBranchEquations(branch, bus1, bus2, creationContext, p1, q1, i1, p2, q2, i2, closedP1, closedQ1, closedI1, @@ -719,6 +725,8 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, createGeneratorReactivePowerControlBranchEquation(branch, bus1, bus2, creationContext, deriveA1, deriveR1); createTransformerPhaseControlEquations(branch, bus1, bus2, creationContext, deriveA1, deriveR1); + + updateBranchEquations(branch); } protected EquationTerm createClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { @@ -745,37 +753,38 @@ protected EquationTerm createClosedBranchSide2Cu return new ClosedBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); } - protected EquationTerm createOpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide2ActiveFlowEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet()); } - protected EquationTerm createOpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide2ReactiveFlowEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet()); } - protected EquationTerm createOpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide2CurrentMagnitudeEquationTerm(branch, bus1, creationContext.getEquationSystem().getVariableSet(), deriveR1); } - protected EquationTerm createOpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide1ActiveFlowEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet()); } - protected EquationTerm createOpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide1ReactiveFlowEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet()); } - protected EquationTerm createOpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, AcEquationSystemCreationContext creationContext) { + return new OpenBranchSide1CurrentMagnitudeEquationTerm(branch, bus2, creationContext.getEquationSystem().getVariableSet()); } - protected static void createImpedantBranchEquations(LfBranch branch, LfBus bus1, LfBus bus2, EquationSystem equationSystem, + protected static void createImpedantBranchEquations(LfBranch branch, LfBus bus1, LfBus bus2, AcEquationSystemCreationContext creationContext, Evaluable p1, Evaluable q1, Evaluable i1, Evaluable p2, Evaluable q2, Evaluable i2, EquationTerm closedP1, EquationTerm closedQ1, EquationTerm closedI1, EquationTerm closedP2, EquationTerm closedQ2, EquationTerm closedI2, EquationTerm openP1, EquationTerm openQ1, EquationTerm openI1, EquationTerm openP2, EquationTerm openQ2, EquationTerm openI2) { + var equationSystem = creationContext.getEquationSystem(); if (closedP1 != null) { equationSystem.getEquation(bus1.getNum(), AcEquationType.BUS_TARGET_P).orElseThrow() .addTerm(closedP1); @@ -926,8 +935,7 @@ private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem equationSystem) { + private void createImpedantBranchEquations(LfBranch branch, AcEquationSystemCreationContext creationContext) { // create zero and non zero impedance branch equations if (branch.isZeroImpedance(LoadFlowModel.AC)) { createNonImpedantBranch(branch, branch.getBus1(), branch.getBus2(), creationContext.getEquationSystem(), branch.isSpanningTreeEdge(LoadFlowModel.AC)); @@ -938,7 +946,7 @@ private void createImpedantBranchEquations(LfBranch branch, private void createBranchesEquations(AcEquationSystemCreationContext creationContext) { for (LfBranch branch : network.getBranches()) { - createImpedantBranchEquations(branch, equationSystem); + createImpedantBranchEquations(branch, creationContext); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index 8b080bbb5f..825b7734ce 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -174,7 +174,7 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE } // positive - createImpedantBranchEquations(branch, bus1, bus2, equationSystem, p1, q1, i1, p2, q2, i2, p1, q1, i1, p2, q2, i2, null, null, null, null, null, null); + createImpedantBranchEquations(branch, bus1, bus2, creationContext, p1, q1, i1, p2, q2, i2, p1, q1, i1, p2, q2, i2, null, null, null, null, null, null); // zero if (ixz1 != null) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java index 96f2138671..f558317cab 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java @@ -21,12 +21,9 @@ abstract class AbstractOpenSide1BranchVectorAcFlowEquationTerm extends AbstractB protected AbstractOpenSide1BranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, AcVariableType variableType, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + VariableSet variableSet) { super(branchVector, branchNum); variables = List.of(variableSet.getVariable(bus2Num, variableType)); - if (deriveA1 || deriveR1) { - throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); - } } protected static double shunt(double y, double cosKsi, double sinKsi, double g1, double b1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java index 51ea888527..b41f8e5d1e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java @@ -21,12 +21,9 @@ abstract class AbstractOpenSide2BranchVectorAcFlowEquationTerm extends AbstractB protected AbstractOpenSide2BranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, AcVariableType variableType, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { + VariableSet variableSet) { super(branchVector, branchNum); variables = List.of(variableSet.getVariable(bus1Num, variableType)); - if (deriveA1 || deriveR1) { - throw new IllegalArgumentException("Variable A1 or R1 on open branch not supported: " + branchNum); - } } protected static double shunt(double y, double cosKsi, double sinKsi, double g2, double b2) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java index eb7988e82e..1bafc25162 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorizedEquationSystemCreator.java @@ -72,33 +72,33 @@ protected EquationTerm createClosedBranchSide2Cu } @Override - protected EquationTerm createOpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchVectorSide2ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide2ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet()); } @Override - protected EquationTerm createOpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchVectorSide2ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide2ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet()); } @Override - protected EquationTerm createOpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchVectorSide2CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide2CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus1.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveR1); } @Override - protected EquationTerm createOpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchVectorSide1ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide1ActiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet()); } @Override - protected EquationTerm createOpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchVectorSide1ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus2, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide1ReactiveFlowEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet()); } @Override - protected EquationTerm createOpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { - return new OpenBranchVectorSide1CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet(), deriveA1, deriveR1); + protected EquationTerm createOpenBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus2, AcEquationSystemCreationContext creationContext) { + return new OpenBranchVectorSide1CurrentMagnitudeEquationTerm(getBranchVector(creationContext), branch.getNum(), bus2.getNum(), creationContext.getEquationSystem().getVariableSet()); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java index b3859947a9..b17ba42c09 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ActiveFlowEquationTerm.java @@ -20,8 +20,8 @@ public class OpenBranchVectorSide1ActiveFlowEquationTerm extends AbstractOpenSid private final Variable v2Var; public OpenBranchVectorSide1ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + VariableSet variableSet) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet); v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java index 9dd6c90d2d..7515646ba9 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1CurrentMagnitudeEquationTerm.java @@ -26,8 +26,8 @@ public class OpenBranchVectorSide1CurrentMagnitudeEquationTerm extends AbstractO private final Variable ph2Var; public OpenBranchVectorSide1CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + VariableSet variableSet) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet); v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); ph2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_PHI); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java index 4e9a973517..e9b273bf46 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide1ReactiveFlowEquationTerm.java @@ -20,8 +20,8 @@ public class OpenBranchVectorSide1ReactiveFlowEquationTerm extends AbstractOpenS private final Variable v2Var; public OpenBranchVectorSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet, deriveA1, deriveR1); + VariableSet variableSet) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus2Num, variableSet); v2Var = variableSet.getVariable(bus2Num, AcVariableType.BUS_V); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java index 3d977833fd..3f10e7b856 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ActiveFlowEquationTerm.java @@ -20,8 +20,8 @@ public class OpenBranchVectorSide2ActiveFlowEquationTerm extends AbstractOpenSid private final Variable v1Var; public OpenBranchVectorSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + VariableSet variableSet) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet); v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java index 5ff165726b..e15a2c350c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java @@ -28,8 +28,8 @@ public class OpenBranchVectorSide2CurrentMagnitudeEquationTerm extends AbstractO private Variable r1Var; public OpenBranchVectorSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + VariableSet variableSet, boolean deriveR1) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet); v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); ph1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_PHI); if (deriveR1) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java index d53bcf1bab..bb351e1a77 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2ReactiveFlowEquationTerm.java @@ -20,8 +20,8 @@ public class OpenBranchVectorSide2ReactiveFlowEquationTerm extends AbstractOpenS private final Variable v1Var; public OpenBranchVectorSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1) { - super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet, deriveA1, deriveR1); + VariableSet variableSet) { + super(branchVector, branchNum, AcVariableType.BUS_V, bus1Num, variableSet); v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); } diff --git a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java index 0eac50b8f5..b918e92753 100644 --- a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java @@ -200,19 +200,19 @@ void branchTest() { // open branch equations branchVector = createBranchVector(null, bus2, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {0.1717595025847833, Double.NaN, Double.NaN, 0.3204828812456483, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchVectorSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchVectorSide1ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet), variables, sv)); assertArrayEquals(new double[] {-0.36364935827807376, Double.NaN, Double.NaN, -0.6785266162875639, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchVectorSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchVectorSide1ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet), variables, sv)); assertArrayEquals(new double[] {0.3752024940555977, Double.NaN, Double.NaN, 0.3500416993992393, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchVectorSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchVectorSide1CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet), variables, sv)); branchVector = createBranchVector(bus1, null, false, false, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); assertArrayEquals(new double[] {0.15639470221220209, Double.NaN, Double.NaN, 0.2919337476186018, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchVectorSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchVectorSide2ActiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet), variables, sv)); assertArrayEquals(new double[] {-0.33122369717493005, Double.NaN, Double.NaN, -0.6182778179094991, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchVectorSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchVectorSide2ReactiveFlowEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet), variables, sv)); assertArrayEquals(new double[] {0.34186721585930596, Double.NaN, Double.NaN, 0.31907275662806295, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN}, - eval(new OpenBranchVectorSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false, false), variables, sv)); + eval(new OpenBranchVectorSide2CurrentMagnitudeEquationTerm(branchVector, branch.getNum(), bus2.getNum(), variableSet, false), variables, sv)); branchVector = createBranchVector(bus1, bus2, true, true, equationSystem, v1Var, v2Var, ph1Var, ph2Var, a1Var, r1Var); // assert current equation is consistent with active and reactive power ones From 0747f19c4190d6fa00cedd17b9820bec65e8a660 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 6 Dec 2023 21:17:23 +0100 Subject: [PATCH 26/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/ac/AcLoadFlowContext.java | 4 +-- ...bstractClosedBranchAcFlowEquationTerm.java | 4 +-- ...anchSide1CurrentMagnitudeEquationTerm.java | 16 ++++++------ ...edBranchSide1ReactiveFlowEquationTerm.java | 14 +++++++---- ...osedBranchSide2ActiveFlowEquationTerm.java | 14 +++++++---- ...anchSide2CurrentMagnitudeEquationTerm.java | 16 ++++++------ ...edBranchSide2ReactiveFlowEquationTerm.java | 14 +++++++---- ...ntCompensatorReactiveFlowEquationTerm.java | 6 ++++- ...tClosedBranchVectorAcFlowEquationTerm.java | 18 ++----------- ...enSide1BranchVectorAcFlowEquationTerm.java | 4 --- ...enSide2BranchVectorAcFlowEquationTerm.java | 4 --- ...ctorSide1CurrentMagnitudeEquationTerm.java | 16 ++---------- ...chVectorSide1ReactiveFlowEquationTerm.java | 25 +++---------------- ...anchVectorSide2ActiveFlowEquationTerm.java | 23 ++--------------- ...ctorSide2CurrentMagnitudeEquationTerm.java | 16 ++---------- ...chVectorSide2ReactiveFlowEquationTerm.java | 24 +++--------------- ...orCompensatorReactiveFlowEquationTerm.java | 5 ++-- 17 files changed, 68 insertions(+), 155 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java b/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java index 4e0dde3366..2e001fd240 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java +++ b/src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowContext.java @@ -6,10 +6,10 @@ */ package com.powsybl.openloadflow.ac; -import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreator; import com.powsybl.openloadflow.ac.equations.AcEquationType; import com.powsybl.openloadflow.ac.equations.AcVariableType; import com.powsybl.openloadflow.ac.equations.asym.AsymmetricalAcEquationSystemCreator; +import com.powsybl.openloadflow.ac.equations.vector.AcVectorizedEquationSystemCreator; import com.powsybl.openloadflow.equations.EquationSystem; import com.powsybl.openloadflow.equations.EquationVector; import com.powsybl.openloadflow.equations.JacobianMatrix; @@ -46,7 +46,7 @@ public JacobianMatrix getJacobianMatrix() { public EquationSystem getEquationSystem() { if (equationSystem == null) { var creator = parameters.isAsymmetrical() ? new AsymmetricalAcEquationSystemCreator(network, parameters.getEquationSystemCreationParameters()) - : new AcEquationSystemCreator(network, parameters.getEquationSystemCreationParameters()); + : new AcVectorizedEquationSystemCreator(network, parameters.getEquationSystemCreationParameters()); equationSystem = creator.create(); } return equationSystem; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java index d5d401a17d..c7f2f61402 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java @@ -38,7 +38,7 @@ public abstract class AbstractClosedBranchAcFlowEquationTerm extends AbstractBra protected final List> variables = new ArrayList<>(); - private static AcVariableType getVoltageMagnitudeType(Fortescue.SequenceType sequenceType) { + public static AcVariableType getVoltageMagnitudeType(Fortescue.SequenceType sequenceType) { return switch (sequenceType) { case POSITIVE -> AcVariableType.BUS_V; case NEGATIVE -> AcVariableType.BUS_V_NEGATIVE; @@ -46,7 +46,7 @@ private static AcVariableType getVoltageMagnitudeType(Fortescue.SequenceType seq }; } - private static AcVariableType getVoltageAngleType(Fortescue.SequenceType sequenceType) { + public static AcVariableType getVoltageAngleType(Fortescue.SequenceType sequenceType) { return switch (sequenceType) { case POSITIVE -> AcVariableType.BUS_PHI; case NEGATIVE -> AcVariableType.BUS_PHI_NEGATIVE; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java index 4a76b0563b..592a8dbe3c 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1CurrentMagnitudeEquationTerm.java @@ -29,17 +29,12 @@ public ClosedBranchSide1CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1 super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - @Override - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + public static double calculateSensi(double y, double ksi, double g1, double b1, + double v1, double ph1, double r1, double a1, double v2, double ph2, + double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { if (dr1 != 0) { throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); } - double v1 = v1(); - double ph1 = ph1(); - double r1 = r1(); - double a1 = a1(); - double v2 = v2(); - double ph2 = ph2(); return di1dph1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dph1 + di1dph2(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dph2 + di1dv1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dv1 @@ -47,6 +42,11 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 + di1da1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * da1; } + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + return calculateSensi(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); + } + private static double theta(double ksi, double a1, double ph2) { return ksi - a1 + A2 + ph2; } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java index d72fe38c7c..8c41893735 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide1ReactiveFlowEquationTerm.java @@ -33,11 +33,10 @@ public ClosedBranchSide1ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, Lf super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta1(ksi, ph1(), a1(), ph2()); + public static double calculateSensi(double y, double ksi, double b1, + double v1, double ph1, double r1, double a1, double v2, double ph2, + double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double theta = theta1(ksi, ph1, a1, ph2); double cosTheta = FastMath.cos(theta); double sinTheta = FastMath.sin(theta); double cosKsi = FastMath.cos(ksi); @@ -49,6 +48,11 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 + dq1dr1(y, cosKsi, b1, v1, r1, v2, cosTheta) * dr1; } + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + return calculateSensi(y, ksi, b1, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); + } + public static double q1(double y, double cosKsi, double b1, double v1, double r1, double v2, double cosTheta) { return r1 * v1 * (-b1 * r1 * v1 + y * r1 * v1 * cosKsi - y * R2 * v2 * cosTheta); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java index 2dd4c5b5b5..50973d3eb6 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java @@ -33,11 +33,10 @@ public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBu super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta2(ksi, ph1(), a1(), ph2()); + public static double calculateSensi(double y, double ksi, double g2, + double v1, double ph1, double r1, double a1, double v2, double ph2, + double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double theta = theta2(ksi, ph1, a1, ph2); double cosTheta = FastMath.cos(theta); double sinTheta = FastMath.sin(theta); return dp2dph1(y, v1, r1, v2, cosTheta) * dph1 @@ -48,6 +47,11 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 + dp2dr1(y, v1, v2, sinTheta) * dr1; } + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + return calculateSensi(y, ksi, g2, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); + } + public static double p2(double y, double sinKsi, double g2, double v1, double r1, double v2, double sinTheta) { return R2 * v2 * (g2 * R2 * v2 - y * r1 * v1 * sinTheta + y * R2 * v2 * sinKsi); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java index 6567ec4f9d..66c009fe4e 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2CurrentMagnitudeEquationTerm.java @@ -29,17 +29,12 @@ public ClosedBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1 super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - @Override - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + public static double calculateSensi(double y, double ksi, double g2, double b2, + double v1, double ph1, double r1, double a1, double v2, double ph2, + double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { if (dr1 != 0) { throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); } - double v1 = v1(); - double ph1 = ph1(); - double r1 = r1(); - double a1 = a1(); - double v2 = v2(); - double ph2 = ph2(); return di2dph1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dph1 + di2dph2(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dph2 + di2dv1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dv1 @@ -47,6 +42,11 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 + di2da1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * da1; } + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + return calculateSensi(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); + } + private static double theta(double ksi, double ph1, double a1) { return ksi + a1 - A2 + ph1; } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java index 46bffea34a..731ceaa511 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ReactiveFlowEquationTerm.java @@ -33,11 +33,10 @@ public ClosedBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, Lf super(branch, bus1, bus2, variableSet, deriveA1, deriveR1, sequenceType); } - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta2(ksi, ph1(), a1(), ph2()); + public static double calculateSensi(double y, double ksi, double b2, + double v1, double ph1, double r1, double a1, double v2, double ph2, + double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + double theta = theta2(ksi, ph1, a1, ph2); double cosTheta = FastMath.cos(theta); double sinTheta = FastMath.sin(theta); return dq2dph1(y, v1, r1, v2, sinTheta) * dph1 @@ -48,6 +47,11 @@ protected double calculateSensi(double dph1, double dph2, double dv1, double dv2 + dq2dr1(y, v1, v2, cosTheta) * dr1; } + @Override + protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { + return calculateSensi(y, ksi, b2, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); + } + public static double q2(double y, double cosKsi, double b2, double v1, double r1, double v2, double cosTheta) { return R2 * v2 * (-b2 * R2 * v2 - y * r1 * v1 * cosTheta + y * R2 * v2 * cosKsi); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java index 0d72cef279..ac81d3b5dc 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/ShuntCompensatorReactiveFlowEquationTerm.java @@ -72,13 +72,17 @@ public double der(Variable variable) { } } + public static double calculateSensi(double v, double b, double dv, double db) { + return dqdv(v, b) * dv + dqdb(v) * db; + } + @Override public double calculateSensi(DenseMatrix dx, int column) { double dv = dx.get(vVar.getRow(), column); double db = bVar != null ? dx.get(bVar.getRow(), column) : 0; double v = v(); double b = b(); - return dqdv(v, b) * dv + dqdb(v) * db; + return calculateSensi(v, b, dv, db); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java index b4d8ed0ab5..2fb27d3166 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractClosedBranchVectorAcFlowEquationTerm.java @@ -16,6 +16,8 @@ import java.util.List; import java.util.Objects; +import static com.powsybl.openloadflow.ac.equations.AbstractClosedBranchAcFlowEquationTerm.getVoltageAngleType; +import static com.powsybl.openloadflow.ac.equations.AbstractClosedBranchAcFlowEquationTerm.getVoltageMagnitudeType; import static com.powsybl.openloadflow.network.PiModel.A2; /** @@ -37,22 +39,6 @@ public abstract class AbstractClosedBranchVectorAcFlowEquationTerm extends Abstr protected final List> variables = new ArrayList<>(); - private static AcVariableType getVoltageMagnitudeType(Fortescue.SequenceType sequenceType) { - return switch (sequenceType) { - case POSITIVE -> AcVariableType.BUS_V; - case NEGATIVE -> AcVariableType.BUS_V_NEGATIVE; - case ZERO -> AcVariableType.BUS_V_ZERO; - }; - } - - private static AcVariableType getVoltageAngleType(Fortescue.SequenceType sequenceType) { - return switch (sequenceType) { - case POSITIVE -> AcVariableType.BUS_PHI; - case NEGATIVE -> AcVariableType.BUS_PHI_NEGATIVE; - case ZERO -> AcVariableType.BUS_PHI_ZERO; - }; - } - protected AbstractClosedBranchVectorAcFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, VariableSet variableSet, boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java index f558317cab..11f7c51606 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide1BranchVectorAcFlowEquationTerm.java @@ -26,10 +26,6 @@ protected AbstractOpenSide1BranchVectorAcFlowEquationTerm(AcBranchVector branchV variables = List.of(variableSet.getVariable(bus2Num, variableType)); } - protected static double shunt(double y, double cosKsi, double sinKsi, double g1, double b1) { - return (g1 + y * sinKsi) * (g1 + y * sinKsi) + (-b1 + y * cosKsi) * (-b1 + y * cosKsi); - } - @Override public List> getVariables() { return variables; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java index b41f8e5d1e..51b4780de9 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AbstractOpenSide2BranchVectorAcFlowEquationTerm.java @@ -26,10 +26,6 @@ protected AbstractOpenSide2BranchVectorAcFlowEquationTerm(AcBranchVector branchV variables = List.of(variableSet.getVariable(bus1Num, variableType)); } - protected static double shunt(double y, double cosKsi, double sinKsi, double g2, double b2) { - return (g2 + y * sinKsi) * (g2 + y * sinKsi) + (-b2 + y * cosKsi) * (-b2 + y * cosKsi); - } - @Override public List> getVariables() { return variables; diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java index 36bb7e0e31..fd223540eb 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1CurrentMagnitudeEquationTerm.java @@ -7,6 +7,7 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ClosedBranchSide1CurrentMagnitudeEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.util.Fortescue; @@ -28,24 +29,11 @@ public ClosedBranchVectorSide1CurrentMagnitudeEquationTerm(AcBranchVector branch @Override protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - if (dr1 != 0) { - throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); - } double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; double g1 = branchVector.g1[num]; double b1 = branchVector.b1[num]; - double v1 = v1(); - double ph1 = ph1(); - double r1 = r1(); - double a1 = a1(); - double v2 = v2(); - double ph2 = ph2(); - return di1dph1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dph1 - + di1dph2(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dph2 - + di1dv1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dv1 - + di1dv2(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * dv2 - + di1da1(y, ksi, g1, b1, v1, ph1, r1, a1, v2, ph2) * da1; + return ClosedBranchSide1CurrentMagnitudeEquationTerm.calculateSensi(y, ksi, g1, b1, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java index dd87c45ea5..0654c3ed99 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide1ReactiveFlowEquationTerm.java @@ -7,15 +7,13 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ClosedBranchSide1ReactiveFlowEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.util.Fortescue; -import net.jafama.FastMath; import java.util.Objects; -import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide1ReactiveFlowEquationTerm.*; - /** * @author Geoffroy Jamgotchian {@literal } */ @@ -27,29 +25,12 @@ public ClosedBranchVectorSide1ReactiveFlowEquationTerm(AcBranchVector branchVect super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchVectorSide1ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); - } - + @Override protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; double b1 = branchVector.b1[num]; - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta1(ksi, ph1(), a1(), ph2()); - double cosTheta = FastMath.cos(theta); - double sinTheta = FastMath.sin(theta); - double cosKsi = FastMath.cos(ksi); - return dq1dph1(y, v1, r1, v2, sinTheta) * dph1 - + dq1dph2(y, v1, r1, v2, sinTheta) * dph2 - + dq1dv1(y, cosKsi, b1, v1, r1, v2, cosTheta) * dv1 - + dq1dv2(y, v1, r1, cosTheta) * dv2 - + dq1da1(y, v1, r1, v2, sinTheta) * da1 - + dq1dr1(y, cosKsi, b1, v1, r1, v2, cosTheta) * dr1; + return ClosedBranchSide1ReactiveFlowEquationTerm.calculateSensi(y, ksi, b1, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java index d470025537..04882a6db8 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ActiveFlowEquationTerm.java @@ -7,15 +7,13 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ClosedBranchSide2ActiveFlowEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.util.Fortescue; -import net.jafama.FastMath; import java.util.Objects; -import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide2ActiveFlowEquationTerm.*; - /** * @author Geoffroy Jamgotchian {@literal } */ @@ -27,28 +25,11 @@ public ClosedBranchVectorSide2ActiveFlowEquationTerm(AcBranchVector branchVector super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchVectorSide2ActiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); - } - protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; double g2 = branchVector.g2[num]; - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta2(ksi, ph1(), a1(), ph2()); - double cosTheta = FastMath.cos(theta); - double sinTheta = FastMath.sin(theta); - return dp2dph1(y, v1, r1, v2, cosTheta) * dph1 - + dp2dph2(y, v1, r1, v2, cosTheta) * dph2 - + dp2dv1(y, r1, v2, sinTheta) * dv1 - + dp2dv2(y, FastMath.sin(ksi), g2, v1, r1, v2, sinTheta) * dv2 - + dp2da1(y, v1, r1, v2, cosTheta) * da1 - + dp2dr1(y, v1, v2, sinTheta) * dr1; + return ClosedBranchSide2ActiveFlowEquationTerm.calculateSensi(y, ksi, g2, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java index 6f4d26ba64..e9c70f2c12 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2CurrentMagnitudeEquationTerm.java @@ -7,6 +7,7 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ClosedBranchSide2CurrentMagnitudeEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.util.Fortescue; @@ -28,24 +29,11 @@ public ClosedBranchVectorSide2CurrentMagnitudeEquationTerm(AcBranchVector branch @Override protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { - if (dr1 != 0) { - throw new IllegalArgumentException("Derivative with respect to r1 not implemented"); - } double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; double g2 = branchVector.g2[num]; double b2 = branchVector.b2[num]; - double v1 = v1(); - double ph1 = ph1(); - double r1 = r1(); - double a1 = a1(); - double v2 = v2(); - double ph2 = ph2(); - return di2dph1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dph1 - + di2dph2(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dph2 - + di2dv1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dv1 - + di2dv2(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * dv2 - + di2da1(y, ksi, g2, b2, v1, ph1, r1, a1, v2, ph2) * da1; + return ClosedBranchSide2CurrentMagnitudeEquationTerm.calculateSensi(y, ksi, g2, b2, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java index a892405ab4..95d6f543dc 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ClosedBranchVectorSide2ReactiveFlowEquationTerm.java @@ -7,15 +7,13 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ClosedBranchSide2ReactiveFlowEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; import com.powsybl.openloadflow.util.Fortescue; -import net.jafama.FastMath; import java.util.Objects; -import static com.powsybl.openloadflow.ac.equations.ClosedBranchSide2ReactiveFlowEquationTerm.*; - /** * @author Geoffroy Jamgotchian {@literal } */ @@ -27,28 +25,12 @@ public ClosedBranchVectorSide2ReactiveFlowEquationTerm(AcBranchVector branchVect super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, Fortescue.SequenceType.POSITIVE); } - public ClosedBranchVectorSide2ReactiveFlowEquationTerm(AcBranchVector branchVector, int branchNum, int bus1Num, int bus2Num, - VariableSet variableSet, boolean deriveA1, boolean deriveR1, - Fortescue.SequenceType sequenceType) { - super(branchVector, branchNum, bus1Num, bus2Num, variableSet, deriveA1, deriveR1, sequenceType); - } - + @Override protected double calculateSensi(double dph1, double dph2, double dv1, double dv2, double da1, double dr1) { double y = branchVector.y[num]; double ksi = branchVector.ksi[num]; double b2 = branchVector.b2[num]; - double v1 = v1(); - double r1 = r1(); - double v2 = v2(); - double theta = theta2(ksi, ph1(), a1(), ph2()); - double cosTheta = FastMath.cos(theta); - double sinTheta = FastMath.sin(theta); - return dq2dph1(y, v1, r1, v2, sinTheta) * dph1 - + dq2dph2(y, v1, r1, v2, sinTheta) * dph2 - + dq2dv1(y, r1, v2, cosTheta) * dv1 - + dq2dv2(y, FastMath.cos(ksi), b2, v1, r1, v2, cosTheta) * dv2 - + dq2da1(y, v1, r1, v2, sinTheta) * da1 - + dq2dr1(y, v1, v2, cosTheta) * dr1; + return ClosedBranchSide2ReactiveFlowEquationTerm.calculateSensi(y, ksi, b2, v1(), ph1(), r1(), a1(), v2(), ph2(), dph1, dph2, dv1, dv2, da1, dr1); } @Override diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java index 801cde5279..fc77b96294 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/ShuntVectorCompensatorReactiveFlowEquationTerm.java @@ -8,6 +8,7 @@ import com.powsybl.math.matrix.DenseMatrix; import com.powsybl.openloadflow.ac.equations.AcVariableType; +import com.powsybl.openloadflow.ac.equations.ShuntCompensatorReactiveFlowEquationTerm; import com.powsybl.openloadflow.equations.Variable; import com.powsybl.openloadflow.equations.VariableSet; @@ -65,9 +66,7 @@ public double der(Variable variable) { public double calculateSensi(DenseMatrix dx, int column) { double dv = dx.get(vVar.getRow(), column); double db = bVar != null ? dx.get(bVar.getRow(), column) : 0; - double v = v(); - double b = b(); - return dqdv(v, b) * dv + dqdb(v) * db; + return ShuntCompensatorReactiveFlowEquationTerm.calculateSensi(v(), b(), dv, db); } @Override From 5d38fc6ecd04dd460564e5638cc0737ddf186bd3 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 13 Dec 2023 21:48:07 +0100 Subject: [PATCH 27/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/vector/AcBranchVector.java | 7 ++++++ .../ac/equations/vector/AcNetworkVector.java | 24 ++++++++++++++++--- ...ctorSide2CurrentMagnitudeEquationTerm.java | 2 +- .../impl/AbstractImpedantLfBranch.java | 2 +- .../openloadflow/VectorEquationsTest.java | 2 ++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java index b56c63e3a6..90692a64bf 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcBranchVector.java @@ -26,6 +26,9 @@ public class AcBranchVector { final int[] bus1Num; final int[] bus2Num; + final boolean[] connected1; + final boolean[] connected2; + final double[] y; final double[] g12; final double[] b12; @@ -91,6 +94,8 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter int size = branches.size(); bus1Num = new int[size]; bus2Num = new int[size]; + connected1 = new boolean[size]; + connected2 = new boolean[size]; y = new double[size]; g12 = new double[size]; b12 = new double[size]; @@ -156,6 +161,8 @@ public AcBranchVector(List branches, AcEquationSystemCreationParameter LfBus bus2 = branch.getBus2(); bus1Num[i] = bus1 != null ? bus1.getNum() : -1; bus2Num[i] = bus2 != null ? bus2.getNum() : -1; + connected1[i] = branch.isConnectedSide1(); + connected2[i] = branch.isConnectedSide2(); PiModel piModel = branch.getPiModel(); if (piModel.getZ() != 0) { y[i] = piModel.getY(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java index d21eb49240..43a6622454 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcNetworkVector.java @@ -7,6 +7,7 @@ package com.powsybl.openloadflow.ac.equations.vector; import com.google.common.base.Stopwatch; +import com.powsybl.iidm.network.TwoSides; import com.powsybl.openloadflow.ac.equations.*; import com.powsybl.openloadflow.equations.*; import com.powsybl.openloadflow.network.*; @@ -139,6 +140,14 @@ public void copyVariablesToBranches() { } } + private boolean isConnectedSide1(int branchNum) { + return branchVector.bus1Num[branchNum] != -1 && branchVector.connected1[branchNum]; + } + + private boolean isConnectedSide2(int branchNum) { + return branchVector.bus2Num[branchNum] != -1 && branchVector.connected2[branchNum]; + } + /** * Update all power flows and their derivatives. */ @@ -149,7 +158,7 @@ public void updatePowerFlows() { var w = new DoubleWrapper(); for (int branchNum = 0; branchNum < branchVector.getSize(); branchNum++) { if (!branchVector.disabled[branchNum]) { - if (branchVector.bus1Num[branchNum] != -1 && branchVector.bus2Num[branchNum] != -1) { + if (isConnectedSide1(branchNum) && isConnectedSide2(branchNum)) { double ph1 = state[branchVector.ph1Row[branchNum]]; double ph2 = state[branchVector.ph2Row[branchNum]]; double a1 = branchVector.a1Row[branchNum] != -1 ? state[branchVector.a1Row[branchNum]] @@ -400,7 +409,7 @@ public void updatePowerFlows() { // i2 branchVector.i2[branchNum] = FastMath.hypot(branchVector.p2[branchNum], branchVector.q2[branchNum]) / v2; - } else if (branchVector.bus1Num[branchNum] != -1) { + } else if (isConnectedSide1(branchNum)) { double v1 = state[branchVector.v1Row[branchNum]]; double r1 = branchVector.r1Row[branchNum] != -1 ? state[branchVector.r1Row[branchNum]] : branchVector.r1[branchNum]; @@ -446,7 +455,7 @@ public void updatePowerFlows() { r1); branchVector.i1[branchNum] = FastMath.hypot(branchVector.p1[branchNum], branchVector.q1[branchNum]) / v1; - } else if (branchVector.bus2Num[branchNum] != -1) { + } else if (isConnectedSide2(branchNum)) { double v2 = state[branchVector.v2Row[branchNum]]; branchVector.p2[branchNum] = OpenBranchSide1ActiveFlowEquationTerm.p2( @@ -517,6 +526,15 @@ public void onDisableChange(LfElement element, boolean disabled) { } } + @Override + public void onBranchConnectionStatusChange(LfBranch branch, TwoSides side, boolean connected) { + if (side == TwoSides.ONE) { + branchVector.connected1[branch.getNum()] = connected; + } else { + branchVector.connected2[branch.getNum()] = connected; + } + } + @Override public void onTapPositionChange(LfBranch branch, int oldPosition, int newPosition) { PiModel piModel = branch.getPiModel(); diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java index e15a2c350c..cca503cc56 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/vector/OpenBranchVectorSide2CurrentMagnitudeEquationTerm.java @@ -33,7 +33,7 @@ public OpenBranchVectorSide2CurrentMagnitudeEquationTerm(AcBranchVector branchVe v1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_V); ph1Var = variableSet.getVariable(bus1Num, AcVariableType.BUS_PHI); if (deriveR1) { - r1Var = variableSet.getVariable(bus1Num, AcVariableType.BRANCH_RHO1); + r1Var = variableSet.getVariable(branchNum, AcVariableType.BRANCH_RHO1); } } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/AbstractImpedantLfBranch.java b/src/main/java/com/powsybl/openloadflow/network/impl/AbstractImpedantLfBranch.java index da318660fd..6296c703b9 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/AbstractImpedantLfBranch.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/AbstractImpedantLfBranch.java @@ -130,7 +130,7 @@ public void setConnectedSide2(boolean connectedSide2) { if (connectedSide2 != this.connectedSide2) { this.connectedSide2 = connectedSide2; for (LfNetworkListener listener : network.getListeners()) { - listener.onBranchConnectionStatusChange(this, TwoSides.TWO, connectedSide1); + listener.onBranchConnectionStatusChange(this, TwoSides.TWO, connectedSide2); } if (!isConnectedSide1() && !isConnectedSide2()) { setDisabled(false); diff --git a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java index b918e92753..215c388155 100644 --- a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java @@ -109,6 +109,8 @@ void setUp() { branch = Mockito.mock(LfBranch.class, ANSWER); Mockito.doReturn(0).when(branch).getNum(); Mockito.doReturn(false).when(branch).isDisabled(); + Mockito.doReturn(true).when(branch).isConnectedSide1(); + Mockito.doReturn(true).when(branch).isConnectedSide2(); PiModel piModel = Mockito.mock(PiModel.class, ANSWER); Mockito.doReturn(piModel).when(branch).getPiModel(); Mockito.doReturn(R).when(piModel).getR(); From 4b5e74e44fb89892157bfa26ae34b4b92d77fd25 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 14 Dec 2023 22:45:40 +0100 Subject: [PATCH 28/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../network/impl/PropagatedContingency.java | 14 ++++++++++++-- .../openloadflow/sa/AcSecurityAnalysis.java | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java index 514a2d00af..99ded2331a 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java @@ -430,6 +430,16 @@ private ContingencyConnectivityLossImpact findBusesAndBranchesImpactedBecauseOfC } } + private boolean willBeConnectedAfterContingencySide1(Map branchesToOpen, LfBranch branch) { + DisabledBranchStatus status = branchesToOpen.get(branch); + return status == null || status == DisabledBranchStatus.SIDE_2; + } + + private boolean willBeConnectedAfterContingencySide2(Map branchesToOpen, LfBranch branch) { + DisabledBranchStatus status = branchesToOpen.get(branch); + return status == null || status == DisabledBranchStatus.SIDE_1; + } + public Optional toLfContingency(LfNetwork network) { // find branch to open because of direct impact of the contingency (including propagation is activated) Map branchesToOpen = findBranchToOpenDirectlyImpactedByContingency(network); @@ -450,10 +460,10 @@ public Optional toLfContingency(LfNetwork network) { boolean otherSideConnected; if (branch.getBus1() == busToLost) { otherSideBus = branch.getBus2(); - otherSideConnected = branch.isConnectedSide2(); + otherSideConnected = branch.isConnectedSide2() && willBeConnectedAfterContingencySide2(branchesToOpen, branch); } else { otherSideBus = branch.getBus1(); - otherSideConnected = branch.isConnectedSide1(); + otherSideConnected = branch.isConnectedSide1() && willBeConnectedAfterContingencySide1(branchesToOpen, branch); } if (busesToLost.contains(otherSideBus) || !otherSideConnected) { addBranchToOpen(branch, DisabledBranchStatus.BOTH_SIDES, branchesToOpen); diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index 978e8e4c56..fddd950869 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -236,7 +236,7 @@ private PostContingencyResult runPostContingencySimulation(LfNetwork network, Ac PreContingencyNetworkResult preContingencyNetworkResult, boolean createResultExtension) { LOGGER.info("Start post contingency '{}' simulation on network {}", lfContingency.getId(), network); LOGGER.debug("Contingency '{}' impact on network {}: remove {} buses, remove {} branches, remove {} generators, shift {} shunts, shift {} loads", - lfContingency.getId(), network, lfContingency.getDisabledNetwork().getBuses(), lfContingency.getDisabledNetwork().getBranches(), + lfContingency.getId(), network, lfContingency.getDisabledNetwork().getBuses(), lfContingency.getDisabledNetwork().getBranchesStatus(), lfContingency.getLostGenerators(), lfContingency.getShuntsShift(), lfContingency.getLostLoads()); Stopwatch stopwatch = Stopwatch.createStarted(); From 9ef48f5c22294250a2a154e0e48c4a516cacee5b Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Thu, 14 Dec 2023 23:05:47 +0100 Subject: [PATCH 29/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcEquationSystemCreator.java | 86 +++++++++++-------- .../ac/equations/AcEquationSystemUpdater.java | 32 ++++--- 2 files changed, 66 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 2afe229c6e..baacf9eaee 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -89,8 +89,8 @@ private void createLoadEquations(LfBus bus, AcEquationSystemCreationContext crea private void createVoltageControlEquations(AcEquationSystemCreationContext creationContext) { for (LfBus bus : network.getBuses()) { createGeneratorVoltageControlEquations(bus, creationContext); - createTransformerVoltageControlEquations(bus, creationContext.getEquationSystem()); - createShuntVoltageControlEquations(bus, creationContext.getEquationSystem()); + createTransformerVoltageControlEquations(bus, creationContext); + createShuntVoltageControlEquations(bus, creationContext); } } @@ -111,7 +111,7 @@ private void createGeneratorVoltageControlEquations(LfBus bus, AcEquationSystemC // create reactive power distribution equations at voltage controller buses createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext, creationParameters); } - updateGeneratorVoltageControl(voltageControl, creationContext.getEquationSystem()); + updateGeneratorVoltageControl(voltageControl, creationContext); } }); } @@ -144,12 +144,14 @@ protected void createGeneratorReactivePowerControlBranchEquation(LfBranch branch equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_Q) .addTerm(q); createGeneratorReactivePowerDistributionEquations(rpc, creationContext, creationParameters); - updateGeneratorReactivePowerControlBranchEquations(rpc, equationSystem); + updateGeneratorReactivePowerControlBranchEquations(rpc, creationContext); }); } } - public static void updateGeneratorReactivePowerControlBranchEquations(GeneratorReactivePowerControl generatorReactivePowerControl, EquationSystem equationSystem) { + public static void updateGeneratorReactivePowerControlBranchEquations(GeneratorReactivePowerControl generatorReactivePowerControl, + AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); LfBranch controlledBranch = generatorReactivePowerControl.getControlledBranch(); List controllerBuses = generatorReactivePowerControl.getControllerBuses() .stream() @@ -226,7 +228,7 @@ private void createShuntEquations(LfBus bus, AcEquationSystemCreationContext cre private void createGeneratorReactivePowerDistributionEquations(Control control, AcEquationSystemCreationContext creationContext, AcEquationSystemCreationParameters creationParameters) { - List controllerBuses = null; + List controllerBuses; if (control instanceof GeneratorVoltageControl generatorVoltageControl) { controllerBuses = generatorVoltageControl.getMergedControllerElements(); } else if (control instanceof GeneratorReactivePowerControl generatorReactivePowerControl) { @@ -265,14 +267,16 @@ public void recreateReactivePowerDistributionEquations(GeneratorVoltageControl v if (!voltageControl.isLocalControl()) { createGeneratorReactivePowerDistributionEquations(voltageControl, creationContext, creationParameters); } - updateGeneratorVoltageControl(voltageControl, equationSystem); + updateGeneratorVoltageControl(voltageControl, creationContext); } static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, - EquationSystem equationSystem, + AcEquationSystemCreationContext creationContext, AcEquationType distrEqType, AcEquationType ctrlEqType) { checkNotDependentVoltageControl(voltageControl); + var equationSystem = creationContext.getEquationSystem(); + LfBus controlledBus = voltageControl.getControlledBus(); List controllerElements = voltageControl.getMergedControllerElements() @@ -379,13 +383,13 @@ private List> createReactiveTerms(L return terms; } - public static void updateGeneratorVoltageControl(GeneratorVoltageControl voltageControl, EquationSystem equationSystem) { + public static void updateGeneratorVoltageControl(GeneratorVoltageControl voltageControl, AcEquationSystemCreationContext creationContext) { checkNotDependentVoltageControl(voltageControl); // ensure reactive keys are up-to-date voltageControl.updateReactiveKeys(); - updateRemoteVoltageControlEquations(voltageControl, equationSystem, AcEquationType.DISTR_Q, AcEquationType.BUS_TARGET_Q); + updateRemoteVoltageControlEquations(voltageControl, creationContext, AcEquationType.DISTR_Q, AcEquationType.BUS_TARGET_Q); } private static void checkNotDependentVoltageControl(VoltageControl voltageControl) { @@ -395,8 +399,9 @@ private static void checkNotDependentVoltageControl(Voltag } private static void createNonImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, - EquationSystem equationSystem, + AcEquationSystemCreationContext creationContext, boolean spanningTreeEdge) { + var equationSystem = creationContext.getEquationSystem(); if (bus1 != null && bus2 != null) { Optional> v1 = equationSystem.getEquation(bus1.getNum(), AcEquationType.BUS_TARGET_V); Optional> v2 = equationSystem.getEquation(bus2.getNum(), AcEquationType.BUS_TARGET_V); @@ -501,10 +506,12 @@ protected void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus } } - public static void updateTransformerPhaseControlEquations(TransformerPhaseControl phaseControl, EquationSystem equationSystem) { + public static void updateTransformerPhaseControlEquations(TransformerPhaseControl phaseControl, AcEquationSystemCreationContext creationContext) { LfBranch controllerBranch = phaseControl.getControllerBranch(); LfBranch controlledBranch = phaseControl.getControlledBranch(); + var equationSystem = creationContext.getEquationSystem(); + if (phaseControl.getMode() == Mode.CONTROLLER) { boolean controlEnabled = !controllerBranch.isDisabled() && !controlledBranch.isDisabled() && controllerBranch.isPhaseControlEnabled(); @@ -524,12 +531,14 @@ public static void updateTransformerPhaseControlEquations(TransformerPhaseContro } } - private static void createTransformerVoltageControlEquations(LfBus bus, EquationSystem equationSystem) { + private static void createTransformerVoltageControlEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> { // add transformer ratio distribution equations - createR1DistributionEquations(voltageControl, equationSystem); + createR1DistributionEquations(voltageControl, creationContext); + + var equationSystem = creationContext.getEquationSystem(); // we also create an equation per controller that will be used later to maintain R1 variable constant for (LfBranch controllerBranch : voltageControl.getMergedControllerElements()) { @@ -537,12 +546,13 @@ private static void createTransformerVoltageControlEquations(LfBus bus, Equation .addTerm(equationSystem.getVariable(controllerBranch.getNum(), AcVariableType.BRANCH_RHO1).createTerm()); } - updateTransformerVoltageControlEquations(voltageControl, equationSystem); + updateTransformerVoltageControlEquations(voltageControl, creationContext); }); } public static void createR1DistributionEquations(TransformerVoltageControl voltageControl, - EquationSystem equationSystem) { + AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); var controllerBranches = voltageControl.getMergedControllerElements(); for (int i = 0; i < controllerBranches.size(); i++) { LfBranch controllerBranch = controllerBranches.get(i); @@ -565,25 +575,28 @@ public static void createR1DistributionEquations(TransformerVoltageControl volta } } - static void updateTransformerVoltageControlEquations(TransformerVoltageControl voltageControl, EquationSystem equationSystem) { - updateRemoteVoltageControlEquations(voltageControl, equationSystem, AcEquationType.DISTR_RHO, AcEquationType.BRANCH_TARGET_RHO1); + static void updateTransformerVoltageControlEquations(TransformerVoltageControl voltageControl, AcEquationSystemCreationContext creationContext) { + updateRemoteVoltageControlEquations(voltageControl, creationContext, AcEquationType.DISTR_RHO, AcEquationType.BRANCH_TARGET_RHO1); } public static void recreateR1DistributionEquations(TransformerVoltageControl voltageControl, - EquationSystem equationSystem) { + AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); for (LfBranch controllerBranch : voltageControl.getMergedControllerElements()) { equationSystem.removeEquation(controllerBranch.getNum(), AcEquationType.DISTR_RHO); } - createR1DistributionEquations(voltageControl, equationSystem); - updateTransformerVoltageControlEquations(voltageControl, equationSystem); + createR1DistributionEquations(voltageControl, creationContext); + updateTransformerVoltageControlEquations(voltageControl, creationContext); } - private static void createShuntVoltageControlEquations(LfBus bus, EquationSystem equationSystem) { + private static void createShuntVoltageControlEquations(LfBus bus, AcEquationSystemCreationContext creationContext) { bus.getShuntVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) .ifPresent(voltageControl -> { // add shunt distribution equations - createShuntSusceptanceDistributionEquations(voltageControl, equationSystem); + createShuntSusceptanceDistributionEquations(voltageControl, creationContext); + + var equationSystem = creationContext.getEquationSystem(); for (LfShunt controllerShunt : voltageControl.getMergedControllerElements()) { // we also create an equation that will be used later to maintain B variable constant @@ -592,12 +605,13 @@ private static void createShuntVoltageControlEquations(LfBus bus, EquationSystem .addTerm(equationSystem.getVariable(controllerShunt.getNum(), AcVariableType.SHUNT_B).createTerm()); } - updateShuntVoltageControlEquations(voltageControl, equationSystem); + updateShuntVoltageControlEquations(voltageControl, creationContext); }); } public static void createShuntSusceptanceDistributionEquations(ShuntVoltageControl voltageControl, - EquationSystem equationSystem) { + AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); var controllerShunts = voltageControl.getMergedControllerElements(); for (LfShunt controllerShunt : controllerShunts) { // shunt b at controller bus i @@ -619,17 +633,18 @@ public static void createShuntSusceptanceDistributionEquations(ShuntVoltageContr } } - static void updateShuntVoltageControlEquations(ShuntVoltageControl voltageControl, EquationSystem equationSystem) { - updateRemoteVoltageControlEquations(voltageControl, equationSystem, AcEquationType.DISTR_SHUNT_B, AcEquationType.SHUNT_TARGET_B); + static void updateShuntVoltageControlEquations(ShuntVoltageControl voltageControl, AcEquationSystemCreationContext creationContext) { + updateRemoteVoltageControlEquations(voltageControl, creationContext, AcEquationType.DISTR_SHUNT_B, AcEquationType.SHUNT_TARGET_B); } public static void recreateShuntSusceptanceDistributionEquations(ShuntVoltageControl voltageControl, - EquationSystem equationSystem) { + AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); for (LfShunt controllerShunt : voltageControl.getMergedControllerElements()) { equationSystem.removeEquation(controllerShunt.getNum(), AcEquationType.DISTR_SHUNT_B); } - createShuntSusceptanceDistributionEquations(voltageControl, equationSystem); - updateShuntVoltageControlEquations(voltageControl, equationSystem); + createShuntSusceptanceDistributionEquations(voltageControl, creationContext); + updateShuntVoltageControlEquations(voltageControl, creationContext); } public static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParameters creationParameters) { @@ -911,7 +926,8 @@ static void updateBranchEquations(LfBranch branch) { } } - private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem equationSystem) { + private static void createHvdcAcEmulationEquations(LfHvdc hvdc, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); EquationTerm p1 = null; EquationTerm p2 = null; if (hvdc.getBus1() != null && hvdc.getBus2() != null) { @@ -938,7 +954,7 @@ private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem pp.onCreate(equationSystem)); - network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(equationSystem, this, creationContext))); + network.addListener(LfNetworkListenerTracer.trace(new AcEquationSystemUpdater(this, creationContext))); } public EquationSystem create() { diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java index 577a7446f1..66c5265bf2 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java @@ -7,7 +7,6 @@ package com.powsybl.openloadflow.ac.equations; import com.powsybl.iidm.network.TwoSides; -import com.powsybl.openloadflow.equations.EquationSystem; import com.powsybl.openloadflow.lf.AbstractEquationSystemUpdater; import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.util.EvaluableConstants; @@ -24,9 +23,8 @@ public class AcEquationSystemUpdater extends AbstractEquationSystemUpdater equationSystem, - AcEquationSystemCreator creator, AcEquationSystemCreationContext creationContext) { - super(equationSystem, LoadFlowModel.AC); + public AcEquationSystemUpdater(AcEquationSystemCreator creator, AcEquationSystemCreationContext creationContext) { + super(creationContext.getEquationSystem(), LoadFlowModel.AC); this.creator = Objects.requireNonNull(creator); this.creationContext = Objects.requireNonNull(creationContext); } @@ -34,14 +32,14 @@ public AcEquationSystemUpdater(EquationSystem eq private void updateVoltageControls(LfBus bus) { LfZeroImpedanceNetwork zn = bus.getZeroImpedanceNetwork(loadFlowModel); if (zn == null) { - bus.getGeneratorVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateGeneratorVoltageControl(voltageControl.getMainVoltageControl(), equationSystem)); - bus.getTransformerVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateTransformerVoltageControlEquations(voltageControl.getMainVoltageControl(), equationSystem)); - bus.getShuntVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateShuntVoltageControlEquations(voltageControl.getMainVoltageControl(), equationSystem)); + bus.getGeneratorVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateGeneratorVoltageControl(voltageControl.getMainVoltageControl(), creationContext)); + bus.getTransformerVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateTransformerVoltageControlEquations(voltageControl.getMainVoltageControl(), creationContext)); + bus.getShuntVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateShuntVoltageControlEquations(voltageControl.getMainVoltageControl(), creationContext)); } else { for (LfBus zb : zn.getGraph().vertexSet()) { - zb.getGeneratorVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateGeneratorVoltageControl(voltageControl.getMainVoltageControl(), equationSystem)); - zb.getTransformerVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateTransformerVoltageControlEquations(voltageControl.getMainVoltageControl(), equationSystem)); - zb.getShuntVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateShuntVoltageControlEquations(voltageControl.getMainVoltageControl(), equationSystem)); + zb.getGeneratorVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateGeneratorVoltageControl(voltageControl.getMainVoltageControl(), creationContext)); + zb.getTransformerVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateTransformerVoltageControlEquations(voltageControl.getMainVoltageControl(), creationContext)); + zb.getShuntVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateShuntVoltageControlEquations(voltageControl.getMainVoltageControl(), creationContext)); } } } @@ -53,12 +51,12 @@ public void onGeneratorVoltageControlChange(LfBus controllerBus, boolean newVolt @Override public void onTransformerPhaseControlChange(LfBranch controllerBranch, boolean newPhaseControlEnabled) { - AcEquationSystemCreator.updateTransformerPhaseControlEquations(controllerBranch.getPhaseControl().orElseThrow(), equationSystem); + AcEquationSystemCreator.updateTransformerPhaseControlEquations(controllerBranch.getPhaseControl().orElseThrow(), creationContext); } @Override public void onGeneratorReactivePowerControlChange(LfBus controllerBus, boolean newReactiveControllerEnabled) { - controllerBus.getGeneratorReactivePowerControl().ifPresent(generatorReactivePowerControl -> AcEquationSystemCreator.updateGeneratorReactivePowerControlBranchEquations(generatorReactivePowerControl, equationSystem)); + controllerBus.getGeneratorReactivePowerControl().ifPresent(generatorReactivePowerControl -> AcEquationSystemCreator.updateGeneratorReactivePowerControlBranchEquations(generatorReactivePowerControl, creationContext)); } @Override @@ -108,14 +106,14 @@ public void onDisableChange(LfElement element, boolean disabled) { bus.getGeneratorVoltageControl().ifPresent(vc -> updateVoltageControls(vc.getControlledBus())); bus.getTransformerVoltageControl().ifPresent(vc -> updateVoltageControls(vc.getControlledBus())); bus.getShuntVoltageControl().ifPresent(vc -> updateVoltageControls(vc.getControlledBus())); - bus.getGeneratorReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateGeneratorReactivePowerControlBranchEquations(reactivePowerControl, equationSystem)); + bus.getGeneratorReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateGeneratorReactivePowerControlBranchEquations(reactivePowerControl, creationContext)); break; case BRANCH: LfBranch branch = (LfBranch) element; AcEquationSystemCreator.updateBranchEquations(branch); branch.getVoltageControl().ifPresent(vc -> updateVoltageControls(vc.getControlledBus())); - branch.getPhaseControl().ifPresent(phaseControl -> AcEquationSystemCreator.updateTransformerPhaseControlEquations(phaseControl, equationSystem)); - branch.getGeneratorReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateGeneratorReactivePowerControlBranchEquations(reactivePowerControl, equationSystem)); + branch.getPhaseControl().ifPresent(phaseControl -> AcEquationSystemCreator.updateTransformerPhaseControlEquations(phaseControl, creationContext)); + branch.getGeneratorReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateGeneratorReactivePowerControlBranchEquations(reactivePowerControl, creationContext)); break; case SHUNT_COMPENSATOR: LfShunt shunt = (LfShunt) element; @@ -136,10 +134,10 @@ private void recreateDistributionEquations(LfZeroImpedanceNetwork network) { .ifPresent(voltageControl -> creator.recreateReactivePowerDistributionEquations(voltageControl, creationContext)); bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, equationSystem)); + .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, creationContext)); bus.getShuntVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateShuntSusceptanceDistributionEquations(voltageControl, equationSystem)); + .ifPresent(voltageControl -> AcEquationSystemCreator.recreateShuntSusceptanceDistributionEquations(voltageControl, creationContext)); } } From 44480b74d81c50418158fdcd374e7bb2d0921a38 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sun, 17 Dec 2023 20:38:18 +0100 Subject: [PATCH 30/32] Wip Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/network/impl/PropagatedContingency.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java index 99ded2331a..337bfd3327 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java @@ -430,12 +430,12 @@ private ContingencyConnectivityLossImpact findBusesAndBranchesImpactedBecauseOfC } } - private boolean willBeConnectedAfterContingencySide1(Map branchesToOpen, LfBranch branch) { + private boolean isConnectedAfterContingencySide1(Map branchesToOpen, LfBranch branch) { DisabledBranchStatus status = branchesToOpen.get(branch); return status == null || status == DisabledBranchStatus.SIDE_2; } - private boolean willBeConnectedAfterContingencySide2(Map branchesToOpen, LfBranch branch) { + private boolean isConnectedAfterContingencySide2(Map branchesToOpen, LfBranch branch) { DisabledBranchStatus status = branchesToOpen.get(branch); return status == null || status == DisabledBranchStatus.SIDE_1; } @@ -460,10 +460,10 @@ public Optional toLfContingency(LfNetwork network) { boolean otherSideConnected; if (branch.getBus1() == busToLost) { otherSideBus = branch.getBus2(); - otherSideConnected = branch.isConnectedSide2() && willBeConnectedAfterContingencySide2(branchesToOpen, branch); + otherSideConnected = branch.isConnectedSide2() && isConnectedAfterContingencySide2(branchesToOpen, branch); } else { otherSideBus = branch.getBus1(); - otherSideConnected = branch.isConnectedSide1() && willBeConnectedAfterContingencySide1(branchesToOpen, branch); + otherSideConnected = branch.isConnectedSide1() && isConnectedAfterContingencySide1(branchesToOpen, branch); } if (busesToLost.contains(otherSideBus) || !otherSideConnected) { addBranchToOpen(branch, DisabledBranchStatus.BOTH_SIDES, branchesToOpen); From 0a4beff19fdb47861cb4c5a8695bad3f74011bdc Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Mon, 19 Aug 2024 21:51:05 +0200 Subject: [PATCH 31/32] Merge Signed-off-by: Geoffroy Jamgotchian --- .../ac/equations/AcEquationSystemCreator.java | 25 +++++++++++-------- .../ac/equations/AcEquationSystemUpdater.java | 4 +-- .../AsymmetricalAcEquationSystemCreator.java | 2 +- .../openloadflow/VectorEquationsTest.java | 7 +++++- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java index 1e4dbb5f79..db5c04a21b 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemCreator.java @@ -553,7 +553,8 @@ protected void createTransformerPhaseControlEquations(LfBranch branch, LfBus bus } } - protected static void createTransformerReactivePowerControlEquations(LfBranch branch, EquationSystem equationSystem) { + protected static void createTransformerReactivePowerControlEquations(LfBranch branch, AcEquationSystemCreationContext creationContext) { + var equationSystem = creationContext.getEquationSystem(); if (branch.isTransformerReactivePowerController()) { // constant R1 equation for sensitivities only EquationTerm r1 = equationSystem.getVariable(branch.getNum(), AcVariableType.BRANCH_RHO1) @@ -636,7 +637,8 @@ static void updateTransformerVoltageControlEquations(TransformerVoltageControl v updateRemoteVoltageControlEquations(voltageControl, creationContext, AcEquationType.DISTR_RHO, AcEquationType.BRANCH_TARGET_RHO1); } - public static void recreateR1DistributionEquations(TransformerVoltageControl voltageControl, + public static void recreateR1DistributionEquations(LfNetwork network, + TransformerVoltageControl voltageControl, AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); for (LfBranch controllerBranch : voltageControl.getMergedControllerElements()) { @@ -694,7 +696,8 @@ static void updateShuntVoltageControlEquations(ShuntVoltageControl voltageContro updateRemoteVoltageControlEquations(voltageControl, creationContext, AcEquationType.DISTR_SHUNT_B, AcEquationType.SHUNT_TARGET_B); } - public static void recreateShuntSusceptanceDistributionEquations(ShuntVoltageControl voltageControl, + public static void recreateShuntSusceptanceDistributionEquations(LfNetwork network, + ShuntVoltageControl voltageControl, AcEquationSystemCreationContext creationContext) { var equationSystem = creationContext.getEquationSystem(); for (LfShunt controllerShunt : voltageControl.getMergedControllerElements()) { @@ -710,7 +713,7 @@ public static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParame } public static boolean isDeriveR1(LfBranch branch) { - return branch.isVoltageController(); + return branch.isVoltageController() || branch.isTransformerReactivePowerController(); } protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, @@ -800,7 +803,7 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, updateBranchEquations(branch); - createTransformerReactivePowerControlEquations(branch, equationSystem); + createTransformerReactivePowerControlEquations(branch, creationContext); } protected EquationTerm createClosedBranchSide1ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, boolean deriveA1, boolean deriveR1, AcEquationSystemCreationContext creationContext) { @@ -1081,27 +1084,27 @@ private List> createActiveInjection if (branch.getBus1() == bus) { LfBus otherSideBus = branch.getBus2(); if (otherSideBus != null) { - p = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus, otherSideBus, variableSet, deriveA1, deriveR1); + p = createClosedBranchSide1ActiveFlowEquationTerm(branch, bus, otherSideBus, deriveA1, deriveR1, creationContext); branch.addAdditionalClosedP1(p); if (branch.isDisconnectionAllowedSide2()) { - openP = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus, variableSet); + openP = createOpenBranchSide2ActiveFlowEquationTerm(branch, bus, creationContext); branch.addAdditionalOpenP1(openP); } } else { - p = new OpenBranchSide2ActiveFlowEquationTerm(branch, bus, variableSet); + p = createOpenBranchSide2ActiveFlowEquationTerm(branch, bus, creationContext); branch.addAdditionalOpenP1(p); } } else { LfBus otherSideBus = branch.getBus1(); if (otherSideBus != null) { - p = new ClosedBranchSide2ActiveFlowEquationTerm(branch, otherSideBus, bus, variableSet, deriveA1, deriveR1); + p = createClosedBranchSide2ActiveFlowEquationTerm(branch, otherSideBus, bus, deriveA1, deriveR1, creationContext); branch.addAdditionalClosedP2(p); if (branch.isDisconnectionAllowedSide1()) { - openP = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus, variableSet); + openP = createOpenBranchSide1ActiveFlowEquationTerm(branch, bus, creationContext); branch.addAdditionalOpenP2(openP); } } else { - p = new OpenBranchSide1ActiveFlowEquationTerm(branch, bus, variableSet); + p = createOpenBranchSide1ActiveFlowEquationTerm(branch, bus, creationContext); branch.addAdditionalOpenP2(p); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java index 024cd7c0a8..76f60b0d52 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/AcEquationSystemUpdater.java @@ -135,10 +135,10 @@ private void recreateDistributionEquations(LfZeroImpedanceNetwork network) { .ifPresent(voltageControl -> creator.recreateReactivePowerDistributionEquations(voltageControl, creationContext)); bus.getTransformerVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(voltageControl, creationContext)); + .ifPresent(voltageControl -> AcEquationSystemCreator.recreateR1DistributionEquations(network.getNetwork(), voltageControl, creationContext)); bus.getShuntVoltageControl() .filter(voltageControl -> voltageControl.getMergeStatus() == VoltageControl.MergeStatus.MAIN) - .ifPresent(voltageControl -> AcEquationSystemCreator.recreateShuntSusceptanceDistributionEquations(voltageControl, creationContext)); + .ifPresent(voltageControl -> AcEquationSystemCreator.recreateShuntSusceptanceDistributionEquations(network.getNetwork(), voltageControl, creationContext)); } } diff --git a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java index 92f6e3d23b..a67ccbd333 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java +++ b/src/main/java/com/powsybl/openloadflow/ac/equations/asym/AsymmetricalAcEquationSystemCreator.java @@ -224,6 +224,6 @@ protected void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2, AcE createTransformerPhaseControlEquations(branch, bus1, bus2, creationContext, deriveA1, deriveR1); - createTransformerReactivePowerControlEquations(branch, equationSystem); + createTransformerReactivePowerControlEquations(branch, creationContext); } } diff --git a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java index 215c388155..be82ba5d42 100644 --- a/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java +++ b/src/test/java/com/powsybl/openloadflow/VectorEquationsTest.java @@ -60,6 +60,7 @@ public Object answer(InvocationOnMock invocation) { private static final double B_SHUNT = 0.2748383993949494; private static final double DROOP = 103.13240312354819; private static final double P_0 = 1.9280906677246095; + private static final double R_HVDC = 0.001; private static final double LOSS_FACTOR_1 = 0.01100000023841858; private static final double LOSS_FACTOR_2 = 0.02400453453002384; private static final double G_SHUNT = 0.0000372472384299244; @@ -142,6 +143,7 @@ private AcBranchVector createBranchVector(LfBus bus1, LfBus bus2, boolean derive Variable a1Var, Variable r1Var) { Mockito.doReturn(deriveA1).when(branch).isPhaseController(); Mockito.doReturn(deriveR1).when(branch).isVoltageController(); + Mockito.doReturn(false).when(branch).isTransformerReactivePowerController(); Mockito.doReturn(bus1).when(branch).getBus1(); Mockito.doReturn(bus2).when(branch).getBus2(); @@ -313,6 +315,9 @@ void hvdcTest() { Mockito.doReturn(false).when(hvdc).isDisabled(); Mockito.doReturn(DROOP).when(hvdc).getDroop(); Mockito.doReturn(P_0).when(hvdc).getP0(); + Mockito.doReturn(R_HVDC).when(hvdc).getR(); + Mockito.doReturn(Double.MAX_VALUE).when(hvdc).getPMaxFromCS1toCS2(); + Mockito.doReturn(Double.MAX_VALUE).when(hvdc).getPMaxFromCS2toCS1(); LfVscConverterStationImpl station1 = Mockito.mock(LfVscConverterStationImpl.class, new RuntimeExceptionAnswer()); LfVscConverterStationImpl station2 = Mockito.mock(LfVscConverterStationImpl.class, new RuntimeExceptionAnswer()); Mockito.doReturn(station1).when(hvdc).getConverterStation1(); @@ -337,7 +342,7 @@ void hvdcTest() { var sv = new StateVector(new double[] {PH_1, PH_2, 0}); - assertArrayEquals(new double[] {-144.1554855266458, 5906.983150087268, -5906.983150087268, Double.NaN, Double.NaN}, + assertArrayEquals(new double[] {-123.36969198623507, 5906.983150087268, -5906.983150087268, Double.NaN, Double.NaN}, eval(new HvdcAcEmulationSide1ActiveFlowEquationTerm(hvdc, bus1, bus2, variableSet), variables, sv)); assertArrayEquals(new double[] {144.20596034441598, -5909.051430021139, 5909.051430021139, Double.NaN, Double.NaN}, eval(new HvdcAcEmulationSide2ActiveFlowEquationTerm(hvdc, bus1, bus2, variableSet), variables, sv)); From 0c2df8deed23ca67672c311bc3accf76feae7879 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Fri, 20 Dec 2024 14:42:10 +0100 Subject: [PATCH 32/32] Fix Signed-off-by: Geoffroy Jamgotchian --- .../powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index 07a08ee6b8..06dba3a69b 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -1298,11 +1298,7 @@ void testViolationOnThreeWindingsTransformersLeg() { SecurityAnalysisResult result = runSecurityAnalysis(network, List.of(), new LoadFlowParameters()); assertEquals(1, result.getPreContingencyResult().getLimitViolationsResult().getLimitViolations().size()); - LimitViolation expected = new LimitViolation("3wt", null, LimitViolationType.CURRENT, "permanent", - 60, 400., 1.0F, 435.0831773201809, TwoSides.TWO); - int compare = LimitViolations.comparator().compare(expected, - result.getPreContingencyResult().getLimitViolationsResult().getLimitViolations().get(0)); - assertEquals(0, compare); + assertEquals(435.083, result.getPreContingencyResult().getLimitViolationsResult().getLimitViolations().get(0).getValue(), LoadFlowAssert.DELTA_I); } @Test