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

Fix multi components DC LF #492

Merged
merged 1 commit into from
Apr 5, 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
35 changes: 15 additions & 20 deletions src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.powsybl.commons.reporter.Report;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.TypedValue;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.openloadflow.dc.equations.DcEquationSystem;
import com.powsybl.openloadflow.dc.equations.DcEquationType;
Expand All @@ -18,6 +19,7 @@
import com.powsybl.openloadflow.network.util.ActivePowerDistribution;
import com.powsybl.openloadflow.network.util.UniformValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.VoltageInitializer;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,8 +37,6 @@ public class DcLoadFlowEngine {

private final DcLoadFlowParameters parameters;

private double[] targetVector;

public <T> DcLoadFlowEngine(T network, LfNetworkLoader<T> networkLoader, DcLoadFlowParameters parameters, Reporter reporter) {
this.networks = LfNetwork.load(network, networkLoader, parameters.getNetworkParameters(), reporter);
this.parameters = Objects.requireNonNull(parameters);
Expand All @@ -47,9 +47,9 @@ public DcLoadFlowEngine(List<LfNetwork> networks, DcLoadFlowParameters parameter
this.parameters = Objects.requireNonNull(parameters);
}

private void distributeSlack(Collection<LfBus> buses) {
private static void distributeSlack(Collection<LfBus> buses, LoadFlowParameters.BalanceType balanceType) {
double mismatch = getActivePowerMismatch(buses);
ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(parameters.getBalanceType(), false);
ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(balanceType, false);
activePowerDistribution.run(buses, mismatch);
}

Expand All @@ -65,17 +65,17 @@ public List<DcLoadFlowResult> run(Reporter reporter) {
return networks.stream().map(n -> run(reporter, n)).collect(Collectors.toList());
}

public DcLoadFlowResult run(Reporter reporter, LfNetwork pNetwork) {
EquationSystem<DcVariableType, DcEquationType> equationSystem = DcEquationSystem.create(pNetwork, parameters.getEquationSystemCreationParameters());
private DcLoadFlowResult run(Reporter reporter, LfNetwork network) {
EquationSystem<DcVariableType, DcEquationType> equationSystem = DcEquationSystem.create(network, parameters.getEquationSystemCreationParameters());

LoadFlowResult.ComponentResult.Status status = LoadFlowResult.ComponentResult.Status.FAILED;
try (JacobianMatrix<DcVariableType, DcEquationType> j = new JacobianMatrix<>(equationSystem, parameters.getMatrixFactory())) {

status = run(equationSystem, j, Collections.emptyList(), Collections.emptyList(), reporter);
status = run(network, parameters, equationSystem, j, Collections.emptyList(), Collections.emptyList(), reporter).getLeft();
} catch (Exception e) {
LOGGER.error("Failed to solve linear system for DC load flow", e);
}
return new DcLoadFlowResult(pNetwork, getActivePowerMismatch(pNetwork.getBuses()), status);
return new DcLoadFlowResult(network, getActivePowerMismatch(network.getBuses()), status);
}

public static void initStateVector(LfNetwork network, EquationSystem<DcVariableType, DcEquationType> equationSystem, VoltageInitializer initializer) {
Expand Down Expand Up @@ -156,22 +156,20 @@ public static void initTarget(Equation<DcVariableType, DcEquationType> equation,
}
}

public LoadFlowResult.ComponentResult.Status run(EquationSystem<DcVariableType, DcEquationType> equationSystem, JacobianMatrix<DcVariableType, DcEquationType> j,
Collection<LfBus> disabledBuses, Collection<LfBranch> disabledBranches,
Reporter reporter) {
// only process main (largest) connected component
LfNetwork network = networks.get(0);

public static Pair<LoadFlowResult.ComponentResult.Status, double[]> run(LfNetwork network, DcLoadFlowParameters parameters,
EquationSystem<DcVariableType, DcEquationType> equationSystem,
JacobianMatrix<DcVariableType, DcEquationType> j,
Collection<LfBus> disabledBuses, Collection<LfBranch> disabledBranches, Reporter reporter) {
initStateVector(network, equationSystem, new UniformValueVoltageInitializer());

Collection<LfBus> remainingBuses = new LinkedHashSet<>(network.getBuses());
remainingBuses.removeAll(disabledBuses);

if (parameters.isDistributedSlack()) {
distributeSlack(remainingBuses);
distributeSlack(remainingBuses, parameters.getBalanceType());
}

this.targetVector = TargetVector.createArray(network, equationSystem, DcLoadFlowEngine::initTarget);
var targetVector = TargetVector.createArray(network, equationSystem, DcLoadFlowEngine::initTarget);

if (!disabledBuses.isEmpty()) {
// set buses injections and transformers to 0
Expand Down Expand Up @@ -225,10 +223,7 @@ public LoadFlowResult.ComponentResult.Status run(EquationSystem<DcVariableType,
.withSeverity(TypedValue.INFO_SEVERITY)
.build());
LOGGER.info("DC load flow completed (status={})", status);
return status;
}

public double[] getTargetVector() {
return targetVector;
return Pair.of(status, targetVector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void prepare(LfNetwork network) {
balanceType,
false);
DcLoadFlowEngine engine = new DcLoadFlowEngine(List.of(network), parameters);
if (engine.run(reporter, network).getStatus() != LoadFlowResult.ComponentResult.Status.CONVERGED) {
if (engine.run(reporter).get(0).getStatus() != LoadFlowResult.ComponentResult.Status.CONVERGED) {
throw new PowsyblException("DC loadflow failed, impossible to initialize voltage angle from DC values");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void apply(LoadFlowParameters parameters) {
for (var e : busesLoadShift.entrySet()) {
LfBus bus = e.getKey();
PowerShift shift = e.getValue();
bus.setLoadTargetP(bus.getLoadTargetP() - getUpdatedLoadP0(bus, parameters, shift.getActive(), shift.getVariableActive()));
bus.setLoadTargetP(bus.getLoadTargetP() - getUpdatedLoadP0(bus, parameters.getBalanceType(), shift.getActive(), shift.getVariableActive()));
bus.setLoadTargetQ(bus.getLoadTargetQ() - shift.getReactive());
bus.getLfLoads().setAbsVariableLoadTargetP(bus.getLfLoads().getAbsVariableLoadTargetP() - Math.abs(shift.getVariableActive()) * PerUnit.SB);
}
Expand All @@ -121,11 +121,11 @@ public void apply(LoadFlowParameters parameters) {
}
}

public static double getUpdatedLoadP0(LfBus bus, LoadFlowParameters parameters, double initialP0, double initialVariableActivePower) {
public static double getUpdatedLoadP0(LfBus bus, LoadFlowParameters.BalanceType balanceType, double initialP0, double initialVariableActivePower) {
double factor = 0.0;
if (parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD) {
if (balanceType == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD) {
factor = Math.abs(initialP0) / (bus.getLfLoads().getAbsVariableLoadTargetP() / PerUnit.SB);
} else if (parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD) {
} else if (balanceType == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD) {
factor = initialVariableActivePower / (bus.getLfLoads().getAbsVariableLoadTargetP() / PerUnit.SB);
}
return initialP0 + (bus.getLoadTargetP() - bus.getInitialLoadTargetP()) * factor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.powsybl.math.matrix.Matrix;
import com.powsybl.math.matrix.MatrixFactory;
import com.powsybl.openloadflow.OpenLoadFlowParameters;
import com.powsybl.openloadflow.dc.DcLoadFlowParameters;
import com.powsybl.openloadflow.equations.Equation;
import com.powsybl.openloadflow.equations.EquationSystem;
import com.powsybl.openloadflow.equations.EquationTerm;
Expand Down Expand Up @@ -584,8 +585,8 @@ protected List<SensitivityFactorGroup<V, E>> createFactorGroups(List<LfSensitivi
return new ArrayList<>(groupIndexedById.values());
}

protected List<ParticipatingElement> getParticipatingElements(Collection<LfBus> buses, LoadFlowParameters loadFlowParameters, OpenLoadFlowParameters openLoadFlowParameters) {
ActivePowerDistribution.Step step = ActivePowerDistribution.getStep(loadFlowParameters.getBalanceType(), openLoadFlowParameters.isLoadPowerFactorConstant());
protected List<ParticipatingElement> getParticipatingElements(Collection<LfBus> buses, LoadFlowParameters.BalanceType balanceType, OpenLoadFlowParameters openLoadFlowParameters) {
ActivePowerDistribution.Step step = ActivePowerDistribution.getStep(balanceType, openLoadFlowParameters.isLoadPowerFactorConstant());
List<ParticipatingElement> participatingElements = step.getParticipatingElements(buses);
ParticipatingElement.normalizeParticipationFactors(participatingElements, "bus");
return participatingElements;
Expand Down Expand Up @@ -995,13 +996,13 @@ public boolean hasTransformerBusTargetVoltage(SensitivityFactorReader factorRead
return hasTransformerBusTargetVoltage.get();
}

public boolean isDistributedSlackOnGenerators(LoadFlowParameters lfParameters) {
public static boolean isDistributedSlackOnGenerators(DcLoadFlowParameters lfParameters) {
return lfParameters.isDistributedSlack()
&& (lfParameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX
|| lfParameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P);
}

public boolean isDistributedSlackOnLoads(LoadFlowParameters lfParameters) {
public static boolean isDistributedSlackOnLoads(DcLoadFlowParameters lfParameters) {
return lfParameters.isDistributedSlack()
&& (lfParameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD
|| lfParameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void analyse(Network network, List<PropagatedContingency> contingencies,

Map<LfBus, Double> slackParticipationByBus;
if (lfParameters.isDistributedSlack()) {
List<ParticipatingElement> participatingElements = getParticipatingElements(lfNetwork.getBuses(), lfParameters, lfParametersExt);
List<ParticipatingElement> participatingElements = getParticipatingElements(lfNetwork.getBuses(), lfParameters.getBalanceType(), lfParametersExt);
slackParticipationByBus = participatingElements.stream().collect(Collectors.toMap(
ParticipatingElement::getLfBus,
element -> -element.getFactor(),
Expand Down Expand Up @@ -315,7 +315,7 @@ public void analyse(Network network, List<PropagatedContingency> contingencies,

if (lfParameters.isDistributedSlack()) {
List<ParticipatingElement> participatingElementsForThisConnectivity = getParticipatingElements(
slackConnectedComponent, lfParameters, lfParametersExt); // will also be used to recompute the loadflow
slackConnectedComponent, lfParameters.getBalanceType(), lfParametersExt); // will also be used to recompute the loadflow
slackParticipationByBusForThisConnectivity = participatingElementsForThisConnectivity.stream().collect(Collectors.toMap(
ParticipatingElement::getLfBus,
element -> -element.getFactor(),
Expand Down
Loading