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

DC sensi refactoring #608

Merged
merged 18 commits into from
Sep 23, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,27 @@ boolean updateConnectivityWeights(Set<LfBus> nonConnectedBuses) {
}
}

protected List<SensitivityFactorGroup<V, E>> createFactorGroups(List<LfSensitivityFactor<V, E>> factors) {
protected static class SensitivityFactorGroupList<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> {

private final List<SensitivityFactorGroup<V, E>> list;

private final boolean multiVariables;

public SensitivityFactorGroupList(List<SensitivityFactorGroup<V, E>> list) {
this.list = Objects.requireNonNull(list);
multiVariables = list.stream().anyMatch(MultiVariablesFactorGroup.class::isInstance);
}

public List<SensitivityFactorGroup<V, E>> getList() {
return list;
}

public boolean hasMultiVariables() {
return multiVariables;
}
}

protected SensitivityFactorGroupList<V, E> createFactorGroups(List<LfSensitivityFactor<V, E>> factors) {
Map<Pair<SensitivityVariableType, String>, SensitivityFactorGroup<V, E>> groupIndexedById = new LinkedHashMap<>(factors.size());
// index factors by variable config
for (LfSensitivityFactor<V, E> factor : factors) {
Expand All @@ -582,7 +602,7 @@ protected List<SensitivityFactorGroup<V, E>> createFactorGroups(List<LfSensitivi
factorGroup.setIndex(index++);
}

return new ArrayList<>(groupIndexedById.values());
return new SensitivityFactorGroupList<>(new ArrayList<>(groupIndexedById.values()));
}

protected List<ParticipatingElement> getParticipatingElements(Collection<LfBus> buses, LoadFlowParameters.BalanceType balanceType, OpenLoadFlowParameters openLoadFlowParameters) {
Expand All @@ -592,14 +612,14 @@ protected List<ParticipatingElement> getParticipatingElements(Collection<LfBus>
return participatingElements;
}

protected DenseMatrix initFactorsRhs(EquationSystem<V, E> equationSystem, List<SensitivityFactorGroup<V, E>> factorsGroups, Map<LfBus, Double> participationByBus) {
DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), factorsGroups.size());
protected DenseMatrix initFactorsRhs(EquationSystem<V, E> equationSystem, SensitivityFactorGroupList<V, E> factorsGroups, Map<LfBus, Double> participationByBus) {
DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), factorsGroups.getList().size());
fillRhsSensitivityVariable(factorsGroups, rhs, participationByBus);
return rhs;
}

protected void fillRhsSensitivityVariable(List<SensitivityFactorGroup<V, E>> factorGroups, Matrix rhs, Map<LfBus, Double> participationByBus) {
for (SensitivityFactorGroup<V, E> factorGroup : factorGroups) {
protected void fillRhsSensitivityVariable(SensitivityFactorGroupList<V, E> factorGroups, Matrix rhs, Map<LfBus, Double> participationByBus) {
for (SensitivityFactorGroup<V, E> factorGroup : factorGroups.getList()) {
factorGroup.fillRhs(rhs, participationByBus);
}
}
Expand Down Expand Up @@ -633,10 +653,10 @@ protected void setPredefinedResults(Collection<LfSensitivityFactor<V, E>> lfFact
}
}

protected boolean rescaleGlsk(List<SensitivityFactorGroup<V, E>> factorGroups, Set<LfBus> nonConnectedBuses) {
protected boolean rescaleGlsk(SensitivityFactorGroupList<V, E> factorGroups, Set<LfBus> nonConnectedBuses) {
boolean rescaled = false;
// compute the corresponding injection (with participation) for each factor
for (SensitivityFactorGroup<V, E> factorGroup : factorGroups) {
for (SensitivityFactorGroup<V, E> factorGroup : factorGroups.getList()) {
if (factorGroup instanceof MultiVariablesFactorGroup) {
MultiVariablesFactorGroup<V, E> multiVariablesFactorGroup = (MultiVariablesFactorGroup<V, E>) factorGroup;
rescaled |= multiVariablesFactorGroup.updateConnectivityWeights(nonConnectedBuses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AcSensitivityAnalysis(MatrixFactory matrixFactory, GraphConnectivityFacto
super(matrixFactory, connectivityFactory);
}

private void calculateSensitivityValues(List<LfSensitivityFactor<AcVariableType, AcEquationType>> lfFactors, List<SensitivityFactorGroup<AcVariableType, AcEquationType>> factorGroups, DenseMatrix factorsState,
private void calculateSensitivityValues(List<LfSensitivityFactor<AcVariableType, AcEquationType>> lfFactors, SensitivityFactorGroupList<AcVariableType, AcEquationType> factorGroups, DenseMatrix factorsState,
int contingencyIndex, SensitivityResultWriter resultWriter) {
Set<LfSensitivityFactor<AcVariableType, AcEquationType>> lfFactorsSet = new HashSet<>(lfFactors);

Expand All @@ -57,7 +57,7 @@ private void calculateSensitivityValues(List<LfSensitivityFactor<AcVariableType,
lfFactors.stream().filter(factor -> factor.getStatus() == LfSensitivityFactor.Status.VALID_ONLY_FOR_FUNCTION)
.forEach(factor -> resultWriter.writeSensitivityValue(factor.getIndex(), contingencyIndex, 0, unscaleFunction(factor, factor.getFunctionReference())));

for (SensitivityFactorGroup<AcVariableType, AcEquationType> factorGroup : factorGroups) {
for (SensitivityFactorGroup<AcVariableType, AcEquationType> factorGroup : factorGroups.getList()) {
for (LfSensitivityFactor<AcVariableType, AcEquationType> factor : factorGroup.getFactors()) {
if (!lfFactorsSet.contains(factor)) {
continue;
Expand Down Expand Up @@ -107,11 +107,11 @@ protected void setFunctionReferences(List<LfSensitivityFactor<AcVariableType, Ac
}

private void calculatePostContingencySensitivityValues(List<LfSensitivityFactor<AcVariableType, AcEquationType>> lfFactors, LfContingency lfContingency,
LfNetwork lfNetwork, AcLoadFlowContext context, List<SensitivityFactorGroup<AcVariableType, AcEquationType>> factorGroups,
LfNetwork lfNetwork, AcLoadFlowContext context, SensitivityFactorGroupList<AcVariableType, AcEquationType> factorGroups,
Map<LfBus, Double> participationByBus,
LoadFlowParameters lfParameters, OpenLoadFlowParameters lfParametersExt,
int contingencyIndex, SensitivityResultWriter resultWriter,
boolean hasTransformerBusTargetVoltage, boolean hasMultiVariables) {
boolean hasTransformerBusTargetVoltage) {
if (lfParameters.isDistributedSlack() && Math.abs(lfContingency.getActivePowerLoss()) > 0) {
ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(lfParameters.getBalanceType(), lfParametersExt.isLoadPowerFactorConstant());
activePowerDistribution.run(lfNetwork, lfContingency.getActivePowerLoss());
Expand All @@ -134,7 +134,7 @@ private void calculatePostContingencySensitivityValues(List<LfSensitivityFactor<
lfNetwork.fixTransformerVoltageControls();
}

if (hasMultiVariables && (!lfContingency.getBusesLoadShift().isEmpty() || !lfContingency.getLostGenerators().isEmpty())) {
if (factorGroups.hasMultiVariables() && (!lfContingency.getBusesLoadShift().isEmpty() || !lfContingency.getLostGenerators().isEmpty())) {
// FIXME. It does not work with a contingency that breaks connectivity and loose an isolate injection.
Set<LfBus> affectedBuses = lfContingency.getLoadAndGeneratorBuses();
rescaleGlsk(factorGroups, affectedBuses);
Expand Down Expand Up @@ -237,11 +237,9 @@ public void analyse(Network network, List<PropagatedContingency> contingencies,
.run();

// index factors by variable group to compute a minimal number of states
List<SensitivityFactorGroup<AcVariableType, AcEquationType>> factorGroups = createFactorGroups(validLfFactors.stream()
SensitivityFactorGroupList<AcVariableType, AcEquationType> factorGroups = createFactorGroups(validLfFactors.stream()
.filter(factor -> factor.getStatus() == LfSensitivityFactor.Status.VALID).collect(Collectors.toList()));

boolean hasMultiVariables = factorGroups.stream().anyMatch(MultiVariablesFactorGroup.class::isInstance);

// compute the participation for each injection factor (+1 on the injection and then -participation factor on all
// buses that contain elements participating to slack distribution

Expand Down Expand Up @@ -270,7 +268,7 @@ public void analyse(Network network, List<PropagatedContingency> contingencies,
// we make the assumption that we ran a loadflow before, and thus this jacobian is the right one

// otherwise, defining the rhs matrix will result in integer overflow
if (factorGroups.size() >= Integer.MAX_VALUE / (context.getEquationSystem().getIndex().getSortedEquationsToSolve().size() * Double.BYTES)) {
if (factorGroups.getList().size() >= Integer.MAX_VALUE / (context.getEquationSystem().getIndex().getSortedEquationsToSolve().size() * Double.BYTES)) {
throw new PowsyblException("Too many factors!");
}

Expand Down Expand Up @@ -335,7 +333,7 @@ public void analyse(Network network, List<PropagatedContingency> contingencies,
postContingencySlackParticipationByBus = Collections.singletonMap(lfNetwork.getSlackBus(), -1d);
}
calculatePostContingencySensitivityValues(contingencyFactors, lfContingency, lfNetwork, context, factorGroups, postContingencySlackParticipationByBus,
lfParameters, lfParametersExt, lfContingency.getIndex(), resultWriter, Boolean.TRUE.equals(hasBusTargetVoltage.getRight()), hasMultiVariables);
lfParameters, lfParametersExt, lfContingency.getIndex(), resultWriter, Boolean.TRUE.equals(hasBusTargetVoltage.getRight()));

networkState.restore();
}, () -> {
Expand Down
Loading