Skip to content

Reduce time spent in eval and der in equation resolution #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
db3beb2
Refacto step 1 - store network data in vectors
vidaldid-rte Jan 21, 2025
d89a002
only one network data update per branch
vidaldid-rte Jan 21, 2025
d3b6259
vectorize p2 - first step (need to support asymetrical)
vidaldid-rte Jan 21, 2025
0492882
fix asymetrical case
vidaldid-rte Jan 21, 2025
78652ac
add some more der terms
vidaldid-rte Jan 22, 2025
de7359f
store active term status in equation
vidaldid-rte Jan 22, 2025
2dc1edd
Revert "store active term status in equation"
vidaldid-rte Jan 23, 2025
f7db0b8
compute derivatives only when needed
vidaldid-rte Jan 23, 2025
f9d6d53
Vectorize derivative for one term - not connected yet to equation
vidaldid-rte Jan 23, 2025
dcd8634
first equation plugged
vidaldid-rte Jan 23, 2025
1533cec
Vetorize der only for now - some test fails to investigate
vidaldid-rte Jan 24, 2025
acd29cf
Add missing notification on equation change
vidaldid-rte Jan 24, 2025
47da172
Vectorize term status
vidaldid-rte Jan 27, 2025
4f7923b
Vectorize equation status
vidaldid-rte Jan 27, 2025
c02bb41
Test some derivatives connected
vidaldid-rte Jan 27, 2025
4369198
Vectcorize der and eval
vidaldid-rte Jan 29, 2025
6516dbe
lazy equation sort by column
vidaldid-rte Jan 29, 2025
51e9ef7
Sort equations by element num
vidaldid-rte Jan 30, 2025
559d9b3
Merge branch 'main' into investig_vect_and_lazy
vidaldid-rte Mar 24, 2025
22d2c4e
fix test on phase shifter without parallel route
vidaldid-rte Mar 24, 2025
d0ac95d
Adapt engine stopping criteria to test precision
vidaldid-rte Mar 24, 2025
6484c01
Reactivate test
vidaldid-rte Mar 24, 2025
ce3f1da
Merge branch 'main' into investig_vect_and_lazy
vidaldid-rte Mar 26, 2025
ae110de
Merge branch 'main' into investig_vect_and_lazy
vidaldid-rte May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Copyright (c) 2019, RTE (http://www.rte-france.com)
/*
* Copyright (c) 2019-2025, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.openloadflow.ac.equations;

import com.powsybl.openloadflow.ac.equations.vector.AcVectorEngine;
import com.powsybl.openloadflow.equations.AbstractElementEquationTerm;
import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.PiModel;
Expand All @@ -16,29 +17,61 @@
*/
abstract class AbstractBranchAcFlowEquationTerm extends AbstractElementEquationTerm<LfBranch, AcVariableType, AcEquationType> {

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 final AcVectorEngine acVectorEnginee;
protected final int branchNum;

protected AbstractBranchAcFlowEquationTerm(LfBranch branch) {
protected AbstractBranchAcFlowEquationTerm(LfBranch branch, AcVectorEngine acVectorEnginee) {
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;
branchNum = branch.getNum();
this.acVectorEnginee = acVectorEnginee;
if (!acVectorEnginee.networkDataInitialized[branchNum]) {
acVectorEnginee.b1[branchNum] = piModel.getB1();
acVectorEnginee.b2[branchNum] = piModel.getB2();
acVectorEnginee.g1[branchNum] = piModel.getG1();
acVectorEnginee.g2[branchNum] = piModel.getG2();
acVectorEnginee.y[branchNum] = piModel.getY();
acVectorEnginee.ksi[branchNum] = piModel.getKsi();
// y12 = g12+j.b12 = 1/(r+j.x)
acVectorEnginee.g12[branchNum] = piModel.getR() * y() * y();
acVectorEnginee.b12[branchNum] = -piModel.getX() * y() * y();
acVectorEnginee.networkDataInitialized[branchNum] = true;
}
}

protected double b1() {
return acVectorEnginee.b1[branchNum];
}

protected double b2() {
return acVectorEnginee.b2[branchNum];
}

protected double g1() {
return acVectorEnginee.g1[branchNum];
}

protected double g2() {
return acVectorEnginee.g2[branchNum];
}

protected double y() {
return acVectorEnginee.y[branchNum];
}

protected double ksi() {
return acVectorEnginee.ksi[branchNum];
}

protected double g12() {
return acVectorEnginee.g12[branchNum];
}

protected double b12() {
return acVectorEnginee.b12[branchNum];
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019, RTE (http://www.rte-france.com)
/*
* Copyright (c) 2019-2025, 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/.
Expand All @@ -8,15 +8,19 @@
package com.powsybl.openloadflow.ac.equations;

import com.powsybl.math.matrix.DenseMatrix;
import com.powsybl.openloadflow.ac.equations.vector.AcVectorEngine;
import com.powsybl.openloadflow.equations.Equation;
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.network.PiModelArray;
import com.powsybl.openloadflow.util.Fortescue;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.DoubleSupplier;

import static com.powsybl.openloadflow.network.PiModel.A2;

Expand All @@ -37,6 +41,12 @@ public abstract class AbstractClosedBranchAcFlowEquationTerm extends AbstractBra

protected final Variable<AcVariableType> r1Var;

private final boolean isArrayPiModel;

private final double a1;

private final double r1;

protected final List<Variable<AcVariableType>> variables = new ArrayList<>();

public static AcVariableType getVoltageMagnitudeType(Fortescue.SequenceType sequenceType) {
Expand All @@ -56,8 +66,8 @@ public static AcVariableType getVoltageAngleType(Fortescue.SequenceType sequence
}

protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, LfBus bus2, VariableSet<AcVariableType> variableSet,
boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType) {
super(branch);
boolean deriveA1, boolean deriveR1, Fortescue.SequenceType sequenceType, AcVectorEngine acVectorEnginee) {
super(branch, acVectorEnginee);
Objects.requireNonNull(bus1);
Objects.requireNonNull(bus2);
Objects.requireNonNull(variableSet);
Expand All @@ -67,6 +77,20 @@ protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, Lf
v2Var = variableSet.getVariable(bus2.getNum(), vType);
ph1Var = variableSet.getVariable(bus1.getNum(), angleType);
ph2Var = variableSet.getVariable(bus2.getNum(), angleType);
// Just equations with V and phi are vectorized
if (vType == AcVariableType.BUS_V) {
acVectorEnginee.v1Var[branch.getNum()] = v1Var;
acVectorEnginee.v2Var[branch.getNum()] = v2Var;
}
if (angleType == AcVariableType.BUS_PHI) {
acVectorEnginee.ph1Var[branch.getNum()] = ph1Var;
acVectorEnginee.ph2Var[branch.getNum()] = ph2Var;
}

isArrayPiModel = branch.getPiModel() instanceof PiModelArray;
a1 = isArrayPiModel ? Double.NaN : branch.getPiModel().getA1();
r1 = isArrayPiModel ? Double.NaN : branch.getPiModel().getR1();

a1Var = deriveA1 ? variableSet.getVariable(branch.getNum(), AcVariableType.BRANCH_ALPHA1) : null;
r1Var = deriveR1 ? variableSet.getVariable(branch.getNum(), AcVariableType.BRANCH_RHO1) : null;
variables.add(v1Var);
Expand All @@ -75,12 +99,38 @@ protected AbstractClosedBranchAcFlowEquationTerm(LfBranch branch, LfBus bus1, Lf
variables.add(ph2Var);
if (a1Var != null) {
variables.add(a1Var);
} else {
acVectorEnginee.a1[branch.getNum()] = branch.getPiModel().getA1();
}
if (r1Var != null) {
variables.add(r1Var);
}
}

@Override
public void setEquation(Equation<AcVariableType, AcEquationType> equation) {
super.setEquation(equation);
if (equation != null) {
acVectorEnginee.addSupplyingTerm(this);
}
}

public DoubleSupplier getR1Supplier() {
if (r1Var != null || isArrayPiModel) {
return () -> r1();
} else {
return null;
}
}

public DoubleSupplier getA1Supplier() {
if (a1Var != null || isArrayPiModel) {
return () -> a1();
} else {
return null;
}
}

public Variable<AcVariableType> getA1Var() {
return a1Var;
}
Expand All @@ -101,12 +151,18 @@ protected double ph2() {
return sv.get(ph2Var.getRow());
}

protected double r1() {
return r1Var != null ? sv.get(r1Var.getRow()) : element.getPiModel().getR1();
public double r1() {
// TODO: Remove test on var row - should not be called if term is inactive
return r1Var != null && r1Var.getRow() >= 0 ? sv.get(r1Var.getRow()) :
isArrayPiModel ? element.getPiModel().getR1() : r1;
// to avoid memory cache miss we don't load the piModel if not necessary
}

protected double a1() {
return a1Var != null ? sv.get(a1Var.getRow()) : element.getPiModel().getA1();
public double a1() {
// TODO remove test >0 - should not be called if term is inactive
return a1Var != null && a1Var.getRow() >= 0 ? sv.get(a1Var.getRow()) :
isArrayPiModel ? element.getPiModel().getA1() : a1;
// to avoid memory cache miss we don't load the piModel if not necessary
}

public static double theta1(double ksi, double ph1, double a1, double ph2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Copyright (c) 2019, RTE (http://www.rte-france.com)
/*
* Copyright (c) 2019-2025, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.openloadflow.ac.equations;

import com.powsybl.openloadflow.ac.equations.vector.AcVectorEngine;
import com.powsybl.openloadflow.equations.Variable;
import com.powsybl.openloadflow.equations.VariableSet;
import com.powsybl.openloadflow.network.LfBranch;
Expand All @@ -22,8 +23,8 @@ abstract class AbstractOpenSide1BranchAcFlowEquationTerm extends AbstractBranchA
protected final List<Variable<AcVariableType>> variables;

protected AbstractOpenSide1BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType,
LfBus bus, VariableSet<AcVariableType> variableSet) {
super(branch);
LfBus bus, VariableSet<AcVariableType> variableSet, AcVectorEngine acVectorEnginee) {
super(branch, acVectorEnginee);
variables = List.of(variableSet.getVariable(bus.getNum(), variableType));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Copyright (c) 2019, RTE (http://www.rte-france.com)
/*
* Copyright (c) 2019-2025, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.openloadflow.ac.equations;

import com.powsybl.openloadflow.ac.equations.vector.AcVectorEngine;
import com.powsybl.openloadflow.equations.Variable;
import com.powsybl.openloadflow.equations.VariableSet;
import com.powsybl.openloadflow.network.LfBranch;
Expand All @@ -22,8 +23,8 @@ abstract class AbstractOpenSide2BranchAcFlowEquationTerm extends AbstractBranchA
protected final List<Variable<AcVariableType>> variables;

protected AbstractOpenSide2BranchAcFlowEquationTerm(LfBranch branch, AcVariableType variableType,
LfBus bus, VariableSet<AcVariableType> variableSet) {
super(branch);
LfBus bus, VariableSet<AcVariableType> variableSet, AcVectorEngine acVectorEngine) {
super(branch, acVectorEngine);
variables = List.of(variableSet.getVariable(bus.getNum(), variableType));
}

Expand Down
Loading