Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Automatic indexing of equation terms #672

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ public static AcLoadFlowParameters createAcParameters(LoadFlowParameters paramet

public static DcLoadFlowParameters createDcParameters(Network network, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt,
MatrixFactory matrixFactory, GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory,
boolean forcePhaseControlOffAndAddAngle1Var, boolean indexTerms) {
var dcParameters = createDcParameters(parameters, parametersExt, matrixFactory, connectivityFactory, forcePhaseControlOffAndAddAngle1Var, indexTerms);
boolean forcePhaseControlOffAndAddAngle1Var) {
var dcParameters = createDcParameters(parameters, parametersExt, matrixFactory, connectivityFactory, forcePhaseControlOffAndAddAngle1Var);
if (parameters.isReadSlackBus()) {
dcParameters.getNetworkParameters().setSlackBusSelector(new NetworkSlackBusSelector(network, dcParameters.getNetworkParameters().getSlackBusSelector()));
}
Expand All @@ -756,7 +756,7 @@ public static DcLoadFlowParameters createDcParameters(Network network, LoadFlowP

public static DcLoadFlowParameters createDcParameters(LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt,
MatrixFactory matrixFactory, GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory,
boolean forcePhaseControlOffAndAddAngle1Var, boolean indexTerms) {
boolean forcePhaseControlOffAndAddAngle1Var) {
SlackBusSelector slackBusSelector = SlackBusSelector.fromMode(parametersExt.getSlackBusSelectionMode(), parametersExt.getSlackBusesIds(), parametersExt.getPlausibleActivePowerLimit());

var networkParameters = new LfNetworkParameters()
Expand Down Expand Up @@ -785,7 +785,6 @@ public static DcLoadFlowParameters createDcParameters(LoadFlowParameters paramet
.setSvcVoltageMonitoring(false);

var equationSystemCreationParameters = new DcEquationSystemCreationParameters(true,
indexTerms,
forcePhaseControlOffAndAddAngle1Var,
parameters.isDcUseTransformerRatio());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private LoadFlowResult runDc(Network network, LoadFlowParameters parameters, Rep
OpenLoadFlowParameters parametersExt = OpenLoadFlowParameters.get(parameters);
OpenLoadFlowParameters.logDc(parameters, parametersExt);

var dcParameters = OpenLoadFlowParameters.createDcParameters(network, parameters, parametersExt, matrixFactory, connectivityFactory, forcePhaseControlOffAndAddAngle1Var, false);
var dcParameters = OpenLoadFlowParameters.createDcParameters(network, parameters, parametersExt, matrixFactory, connectivityFactory, forcePhaseControlOffAndAddAngle1Var);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public static EquationSystem<AcVariableType, AcEquationType> create(LfNetwork ne
Objects.requireNonNull(network);
Objects.requireNonNull(creationParameters);

EquationSystem<AcVariableType, AcEquationType> equationSystem = new EquationSystem<>(true);
EquationSystem<AcVariableType, AcEquationType> equationSystem = new EquationSystem<>();

createBusesEquations(network, equationSystem, creationParameters);
createBranchesEquations(network, equationSystem, creationParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void prepare(LfNetwork network) {
List<BusDcState> busStates = distributedSlack ? ElementState.save(network.getBuses(), BusDcState::save) : null;

DcLoadFlowParameters parameters = new DcLoadFlowParameters(networkParameters,
new DcEquationSystemCreationParameters(false, false, false, useTransformerRatio),
new DcEquationSystemCreationParameters(false, false, useTransformerRatio),
matrixFactory,
distributedSlack,
balanceType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static void createBranches(LfNetwork network, EquationSystem<DcVariableT

public static EquationSystem<DcVariableType, DcEquationType> create(LfNetwork network, DcEquationSystemCreationParameters creationParameters,
boolean withListener) {
EquationSystem<DcVariableType, DcEquationType> equationSystem = new EquationSystem<>(creationParameters.isIndexTerms());
EquationSystem<DcVariableType, DcEquationType> equationSystem = new EquationSystem<>();

createBuses(network, equationSystem);
createBranches(network, equationSystem, creationParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class DcEquationSystemCreationParameters {

private final boolean updateFlows;

private final boolean indexTerms;

/**
* The purpose of this option is to add a constant a1 var to the equation system, to calculate sensitivity regarding
* phase.
Expand All @@ -23,10 +21,9 @@ public class DcEquationSystemCreationParameters {

private final boolean useTransformerRatio;

public DcEquationSystemCreationParameters(boolean updateFlows, boolean indexTerms, boolean forcePhaseControlOffAndAddAngle1Var,
public DcEquationSystemCreationParameters(boolean updateFlows, boolean forcePhaseControlOffAndAddAngle1Var,
boolean useTransformerRatio) {
this.updateFlows = updateFlows;
this.indexTerms = indexTerms;
this.forcePhaseControlOffAndAddAngle1Var = forcePhaseControlOffAndAddAngle1Var;
this.useTransformerRatio = useTransformerRatio;
}
Expand All @@ -35,10 +32,6 @@ public boolean isUpdateFlows() {
return updateFlows;
}

public boolean isIndexTerms() {
return indexTerms;
}

public boolean isForcePhaseControlOffAndAddAngle1Var() {
return forcePhaseControlOffAndAddAngle1Var;
}
Expand All @@ -51,7 +44,6 @@ public boolean isUseTransformerRatio() {
public String toString() {
return "DcEquationSystemCreationParameters(" +
"updateFlows=" + updateFlows +
", indexTerms=" + indexTerms +
", forcePhaseControlOffAndAddAngle1Var=" + forcePhaseControlOffAndAddAngle1Var +
", useTransformerRatio=" + useTransformerRatio +
')';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.powsybl.math.matrix.DenseMatrix;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -36,6 +38,11 @@ public void setStateVector(StateVector sv) {
this.sv = Objects.requireNonNull(sv);
}

@Override
public List<EquationTerm<V, E>> getChildren() {
return Collections.emptyList();
}

@Override
public Equation<V, E> getEquation() {
return equation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
*/
public class EquationSystem<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> {

private final boolean indexTerms;

private final Map<Pair<Integer, E>, Equation<V, E>> equations = new HashMap<>();

private final Map<Pair<ElementType, Integer>, List<Equation<V, E>>> equationsByElement = new HashMap<>();

private final Map<Pair<ElementType, Integer>, List<EquationTerm<V, E>>> equationTermsByElement = new HashMap<>();
private Map<Pair<ElementType, Integer>, List<EquationTerm<V, E>>> equationTermsByElement;

private final List<EquationSystemListener<V, E>> listeners = new ArrayList<>();

Expand All @@ -41,16 +39,11 @@ public class EquationSystem<V extends Enum<V> & Quantity, E extends Enum<E> & Qu
private final EquationSystemIndex<V, E> index;

public EquationSystem() {
this(false);
}

public EquationSystem(boolean indexTerms) {
this(new VariableSet<>(), indexTerms);
this(new VariableSet<>());
}

public EquationSystem(VariableSet<V> variableSet, boolean indexTerms) {
public EquationSystem(VariableSet<V> variableSet) {
this.variableSet = Objects.requireNonNull(variableSet);
this.indexTerms = indexTerms;
index = new EquationSystemIndex<>(this);
}

Expand All @@ -74,21 +67,38 @@ public Collection<Equation<V, E>> getEquations() {
return equations.values();
}

void addEquationTerm(EquationTerm<V, E> equationTerm) {
if (indexTerms) {
Objects.requireNonNull(equationTerm);
Pair<ElementType, Integer> element = Pair.of(equationTerm.getElementType(), equationTerm.getElementNum());
equationTermsByElement.computeIfAbsent(element, k -> new ArrayList<>())
.add(equationTerm);
private void indexTerm(EquationTerm<V, E> equationTerm) {
if (equationTermsByElement != null) {
if (equationTerm.getElementType() != null && equationTerm.getElementNum() != -1) {
Pair<ElementType, Integer> element = Pair.of(equationTerm.getElementType(), equationTerm.getElementNum());
equationTermsByElement.computeIfAbsent(element, k -> new ArrayList<>())
.add(equationTerm);
}
for (EquationTerm<V, E> child : equationTerm.getChildren()) {
indexTerm(child);
}
}
}

private void indexAllTerms() {
if (equationTermsByElement == null) {
equationTermsByElement = new HashMap<>();
for (var equation : equations.values()) {
for (var term : equation.getTerms()) {
indexTerm(term);
}
}
}
}

void addEquationTerm(EquationTerm<V, E> equationTerm) {
indexTerm(equationTerm);
attach(equationTerm);
}

public List<EquationTerm<V, E>> getEquationTerms(ElementType elementType, int elementNum) {
if (!indexTerms) {
throw new PowsyblException("Equations terms have not been indexed");
}
Objects.requireNonNull(elementType);
indexAllTerms();
Pair<ElementType, Integer> element = Pair.of(elementType, elementNum);
return equationTermsByElement.getOrDefault(element, Collections.emptyList());
}
Expand Down
58 changes: 9 additions & 49 deletions src/main/java/com/powsybl/openloadflow/equations/EquationTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.DoubleSupplier;
Expand Down Expand Up @@ -39,6 +40,11 @@ class MultiplyByScalarEquationTerm<V extends Enum<V> & Quantity, E extends Enum<
term.setSelf(this);
}

@Override
public List<EquationTerm<V, E>> getChildren() {
return Collections.singletonList(term);
}

@Override
public Equation<V, E> getEquation() {
return term.getEquation();
Expand Down Expand Up @@ -66,12 +72,12 @@ public void setSelf(EquationTerm<V, E> self) {

@Override
public ElementType getElementType() {
return term.getElementType();
return null;
}

@Override
public int getElementNum() {
return term.getElementNum();
return -1;
}

@Override
Expand Down Expand Up @@ -125,53 +131,7 @@ static <V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> EquationTerm
return new MultiplyByScalarEquationTerm<>(term, scalar);
}

class VariableEquationTerm<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> extends AbstractEquationTerm<V, E> {

private final List<Variable<V>> variables;

VariableEquationTerm(Variable<V> variable) {
this.variables = List.of(Objects.requireNonNull(variable));
}

private Variable<V> getVariable() {
return variables.get(0);
}

@Override
public ElementType getElementType() {
return getVariable().getType().getElementType();
}

@Override
public int getElementNum() {
return getVariable().getElementNum();
}

@Override
public List<Variable<V>> getVariables() {
return variables;
}

@Override
public double eval() {
return sv.get(getVariable().getRow());
}

@Override
public double der(Variable<V> variable) {
return 1;
}

@Override
public double calculateSensi(DenseMatrix dx, int column) {
return dx.get(getVariable().getRow(), column);
}

@Override
public void write(Writer writer) throws IOException {
getVariable().write(writer);
}
}
List<EquationTerm<V, E>> getChildren();

Equation<V, E> getEquation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void write(Writer writer) throws IOException {
}

public <E extends Enum<E> & Quantity> EquationTerm<V, E> createTerm() {
return new EquationTerm.VariableEquationTerm<>(this);
return new VariableEquationTerm<>(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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.equations;

import com.powsybl.math.matrix.DenseMatrix;
import com.powsybl.openloadflow.network.ElementType;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Objects;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class VariableEquationTerm<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> extends AbstractEquationTerm<V, E> {

private final List<Variable<V>> variables;

public VariableEquationTerm(Variable<V> variable) {
this.variables = List.of(Objects.requireNonNull(variable));
}

private Variable<V> getVariable() {
return variables.get(0);
}

@Override
public ElementType getElementType() {
return getVariable().getType().getElementType();
}

@Override
public int getElementNum() {
return getVariable().getElementNum();
}

@Override
public List<Variable<V>> getVariables() {
return variables;
}

@Override
public double eval() {
return sv.get(getVariable().getRow());
}

@Override
public double der(Variable<V> variable) {
return 1;
}

@Override
public double calculateSensi(DenseMatrix dx, int column) {
return dx.get(getVariable().getRow(), column);
}

@Override
public void write(Writer writer) throws IOException {
getVariable().write(writer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private List<OperatorStrategyResult> createOperatorStrategyResults(DcSecurityAna
boolean breakers = !(allSwitchesToOpen.isEmpty() && allSwitchesToClose.isEmpty());

var dcParameters = OpenLoadFlowParameters.createDcParameters(network, context.getParameters().getLoadFlowParameters(),
parametersExt, matrixFactory, connectivityFactory, false, true);
parametersExt, matrixFactory, connectivityFactory, false);
dcParameters.getNetworkParameters().setBreakers(breakers);

try (LfNetworkList lfNetworks = Networks.load(network, dcParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, Reporter.NO_OP)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ private JacobianMatrix<DcVariableType, DcEquationType> createJacobianMatrix(LfNe
private static DcLoadFlowParameters createDcLoadFlowParameters(LfNetworkParameters networkParameters, MatrixFactory matrixFactory,
LoadFlowParameters lfParameters) {
var equationSystemCreationParameters = new DcEquationSystemCreationParameters(true,
true,
true,
lfParameters.isDcUseTransformerRatio());

Expand Down
Loading