Skip to content

Commit

Permalink
Functional logging refactoring (#574)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@rte-france.com>
  • Loading branch information
geofjamg authored Jul 1, 2022
1 parent d715410 commit 9153bfb
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.impl.LfNetworkLoaderImpl;
import com.powsybl.openloadflow.network.impl.Networks;
import com.powsybl.openloadflow.util.Markers;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.util.PowsyblOpenLoadFlowVersion;
import com.powsybl.openloadflow.util.ProviderConstants;
import com.powsybl.openloadflow.util.*;
import com.powsybl.tools.PowsyblCoreVersion;
import net.jafama.FastMath;
import org.slf4j.Logger;
Expand Down Expand Up @@ -216,8 +213,7 @@ public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager

LOGGER.info("Version: {}", new PowsyblOpenLoadFlowVersion());

Reporter lfReporter = reporter.createSubReporter("loadFlow", "Load flow on network ${networkId}",
"networkId", network.getId());
Reporter lfReporter = Reports.createLoadFlowReporter(reporter, network.getId());

return CompletableFuture.supplyAsync(() -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
package com.powsybl.openloadflow.ac;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.reporter.Report;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.TypedValue;
import com.powsybl.openloadflow.OpenLoadFlowReportConstants;
import com.powsybl.openloadflow.ac.outerloop.OuterLoop;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopContext;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopStatus;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.network.util.ActivePowerDistribution;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.util.Reports;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -53,13 +51,7 @@ public OuterLoopStatus check(OuterLoopContext context, Reporter reporter) {
ActivePowerDistribution.Result result = activePowerDistribution.run(context.getNetwork(), slackBusActivePowerMismatch);

if (Math.abs(result.getRemainingMismatch()) > ActivePowerDistribution.P_RESIDUE_EPS) {
reporter.report(Report.builder()
.withKey("mismatchDistributionFailure")
.withDefaultMessage("Iteration ${iteration}: failed to distribute slack bus active power mismatch, ${mismatch} MW remains")
.withValue("iteration", context.getIteration())
.withTypedValue("mismatch", result.getRemainingMismatch() * PerUnit.SB, OpenLoadFlowReportConstants.MISMATCH_TYPED_VALUE)
.withSeverity(TypedValue.ERROR_SEVERITY)
.build());
Reports.reportMismatchDistributionFailure(reporter, context.getIteration(), result.getRemainingMismatch() * PerUnit.SB);

if (throwsExceptionInCaseOfFailure) {
throw new PowsyblException("Failed to distribute slack bus active power mismatch, "
Expand All @@ -70,27 +62,17 @@ public OuterLoopStatus check(OuterLoopContext context, Reporter reporter) {

return OuterLoopStatus.STABLE;
} else {
reporter.report(Report.builder()
.withKey("mismatchDistributionSuccess")
.withDefaultMessage("Iteration ${iteration}: slack bus active power (${initialMismatch} MW) distributed in ${nbIterations} iterations")
.withValue("iteration", context.getIteration())
.withTypedValue("initialMismatch", slackBusActivePowerMismatch * PerUnit.SB, OpenLoadFlowReportConstants.MISMATCH_TYPED_VALUE)
.withValue("nbIterations", result.getIteration())
.withSeverity(TypedValue.INFO_SEVERITY)
.build());
Reports.reportMismatchDistributionSuccess(reporter, context.getIteration(), slackBusActivePowerMismatch * PerUnit.SB, result.getIteration());

LOGGER.info("Slack bus active power ({} MW) distributed in {} iterations",
slackBusActivePowerMismatch * PerUnit.SB, result.getIteration());

return OuterLoopStatus.UNSTABLE;
}
}

reporter.report(Report.builder()
.withKey("NoMismatchDistribution")
.withDefaultMessage("Iteration ${iteration}: already balanced")
.withValue("iteration", context.getIteration())
.withSeverity(TypedValue.INFO_SEVERITY)
.build());
Reports.reportNoMismatchDistribution(reporter, context.getIteration());

LOGGER.debug("Already balanced");

return OuterLoopStatus.STABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
*/
package com.powsybl.openloadflow.ac;

import com.powsybl.commons.reporter.Report;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.TypedValue;
import com.powsybl.openloadflow.ac.outerloop.OuterLoop;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopContext;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopStatus;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.VoltageControl;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.util.Reports;
import org.apache.commons.lang3.mutable.MutableInt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,13 +105,8 @@ private boolean switchPvPq(List<PvToPqBus> pvToPqBuses, int remainingPvBusCount,
}
}

reporter.report(Report.builder()
.withKey("switchPvPq")
.withDefaultMessage("${pvToPqBuses} buses switched PV -> PQ ({remainingPvBuses} bus remains PV}")
.withValue("pvToPqBuses", pvToPqBuses.size())
.withValue("remainingPvBuses", modifiedRemainingPvBusCount)
.withSeverity(TypedValue.INFO_SEVERITY)
.build());
Reports.reportPvToPqBuses(reporter, pvToPqBuses.size(), modifiedRemainingPvBusCount);

LOGGER.info("{} buses switched PV -> PQ ({} bus remains PV}", pvToPqBuses.size(), modifiedRemainingPvBusCount);

return done;
Expand Down Expand Up @@ -161,13 +155,8 @@ private boolean switchPqPv(List<PqToPvBus> pqToPvBuses, Reporter reporter) {
}
}

reporter.report(Report.builder()
.withKey("switchPqPv")
.withDefaultMessage("${pqToPvBuses} buses switched PQ -> PV ({blockedPqBuses} buses blocked PQ because have reach max number of switch)")
.withValue("pqToPvBuses", pqPvSwitchCount)
.withValue("blockedPqBuses", pqToPvBuses.size() - pqPvSwitchCount)
.withSeverity(TypedValue.INFO_SEVERITY)
.build());
Reports.reportPqToPvBuses(reporter, pqPvSwitchCount, pqToPvBuses.size() - pqPvSwitchCount);

LOGGER.info("{} buses switched PQ -> PV ({} buses blocked PQ because have reach max number of switch)",
pqPvSwitchCount, pqToPvBuses.size() - pqPvSwitchCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.powsybl.openloadflow.ac.nr;

import com.powsybl.commons.reporter.Reporter;
import com.powsybl.math.matrix.MatrixException;
import com.powsybl.openloadflow.ac.equations.AcEquationType;
import com.powsybl.openloadflow.ac.equations.AcVariableType;
Expand Down Expand Up @@ -164,8 +163,7 @@ public void updateNetwork() {
}
}

public NewtonRaphsonResult run(VoltageInitializer voltageInitializer, Reporter reporter) {
Objects.requireNonNull(reporter);
public NewtonRaphsonResult run(VoltageInitializer voltageInitializer) {

// initialize state vector
initStateVector(network, equationSystem, voltageInitializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.openloadflow.network.LfNetworkLoader;
import com.powsybl.openloadflow.network.util.PreviousValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.VoltageInitializer;
import com.powsybl.openloadflow.util.Reports;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
Expand Down Expand Up @@ -50,9 +51,8 @@ private static class RunningContext {
private final Map<String, MutableInt> outerLoopIterationByType = new HashMap<>();
}

private void runOuterLoop(OuterLoop outerLoop, OuterLoopContextImpl outerLoopContext, NewtonRaphson newtonRaphson, RunningContext runningContext,
Reporter reporter) {
Reporter olReporter = reporter.createSubReporter("OuterLoop", "Outer loop ${outerLoopType}", "outerLoopType", outerLoop.getType());
private void runOuterLoop(OuterLoop outerLoop, OuterLoopContextImpl outerLoopContext, NewtonRaphson newtonRaphson, RunningContext runningContext) {
Reporter olReporter = Reports.createOuterLoopReporter(outerLoopContext.getNetwork().getReporter(), outerLoop.getType());

// for each outer loop re-run Newton-Raphson until stabilization
OuterLoopStatus outerLoopStatus;
Expand All @@ -68,7 +68,7 @@ private void runOuterLoop(OuterLoop outerLoop, OuterLoopContextImpl outerLoopCon
LOGGER.debug("Start outer loop iteration {} (name='{}')", outerLoopIteration, outerLoop.getType());

// if not yet stable, restart Newton-Raphson
runningContext.lastNrResult = newtonRaphson.run(new PreviousValueVoltageInitializer(), reporter);
runningContext.lastNrResult = newtonRaphson.run(new PreviousValueVoltageInitializer());
if (runningContext.lastNrResult.getStatus() != NewtonRaphsonStatus.CONVERGED) {
return;
}
Expand All @@ -79,10 +79,6 @@ private void runOuterLoop(OuterLoop outerLoop, OuterLoopContextImpl outerLoopCon
}

public AcLoadFlowResult run() {
return run(Reporter.NO_OP);
}

public AcLoadFlowResult run(Reporter reporter) {
LOGGER.info("Start AC loadflow on network {}", context.getNetwork());

VoltageInitializer voltageInitializer = context.getParameters().getVoltageInitializer();
Expand All @@ -108,7 +104,7 @@ public AcLoadFlowResult run(Reporter reporter) {
}

// run initial Newton-Raphson
runningContext.lastNrResult = newtonRaphson.run(voltageInitializer, reporter);
runningContext.lastNrResult = newtonRaphson.run(voltageInitializer);
double initialSlackBusActivePowerMismatch = runningContext.lastNrResult.getSlackBusActivePowerMismatch();

// continue with outer loops only if initial Newton-Raphson succeed
Expand All @@ -121,7 +117,7 @@ public AcLoadFlowResult run(Reporter reporter) {

// outer loops are nested: inner most loop first in the list, outer most loop last
for (var outerLoopAndContext : outerLoopsAndContexts) {
runOuterLoop(outerLoopAndContext.getLeft(), outerLoopAndContext.getRight(), newtonRaphson, runningContext, reporter);
runOuterLoop(outerLoopAndContext.getLeft(), outerLoopAndContext.getRight(), newtonRaphson, runningContext);

// continue with next outer loop only if last Newton-Raphson succeed
if (runningContext.lastNrResult.getStatus() != NewtonRaphsonStatus.CONVERGED) {
Expand Down Expand Up @@ -161,7 +157,7 @@ public static <T> List<AcLoadFlowResult> run(T network, LfNetworkLoader<T> netwo
if (n.isValid()) {
try (AcLoadFlowContext context = new AcLoadFlowContext(n, parameters)) {
return new AcloadFlowEngine(context)
.run(reporter);
.run();
}
}
return new AcLoadFlowResult(n, 0, 0, NewtonRaphsonStatus.NO_CALCULATION, Double.NaN, 0);
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/
package com.powsybl.openloadflow.dc;

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.math.matrix.MatrixException;
Expand All @@ -20,6 +18,7 @@
import com.powsybl.openloadflow.network.util.ActivePowerDistribution;
import com.powsybl.openloadflow.network.util.UniformValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.VoltageInitializer;
import com.powsybl.openloadflow.util.Reports;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -198,12 +197,8 @@ public static Pair<LoadFlowResult.ComponentResult.Status, double[]> run(LfNetwor
status = LoadFlowResult.ComponentResult.Status.CONVERGED;
} catch (MatrixException e) {
status = LoadFlowResult.ComponentResult.Status.FAILED;
reporter.report(Report.builder()
.withKey("loadFlowFailure")
.withDefaultMessage("Failed to solve linear system for DC load flow: ${errorMessage}")
.withValue("errorMessage", e.getMessage())
.withSeverity(TypedValue.ERROR_SEVERITY)
.build());

Reports.reportDcLfSolverFailure(reporter, e.getMessage());
LOGGER.error("Failed to solve linear system for DC load flow", e);
}

Expand All @@ -217,12 +212,7 @@ public static Pair<LoadFlowResult.ComponentResult.Status, double[]> run(LfNetwor
}
}

reporter.report(Report.builder()
.withKey("loadFlowCompleted")
.withDefaultMessage("DC load flow completed (status=${lfStatus})")
.withValue("lfStatus", status.toString())
.withSeverity(TypedValue.INFO_SEVERITY)
.build());
Reports.reportDcLfComplete(reporter, status.toString());
LOGGER.info("DC load flow completed (status={})", status);

return Pair.of(status, targetVector);
Expand Down
48 changes: 18 additions & 30 deletions src/main/java/com/powsybl/openloadflow/network/LfNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.common.base.Stopwatch;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.reporter.Report;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.TypedValue;
import com.powsybl.openloadflow.graph.GraphDecrementalConnectivity;
import com.powsybl.openloadflow.graph.GraphDecrementalConnectivityFactory;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.util.Reports;
import net.jafama.FastMath;
import org.jgrapht.Graph;
import org.jgrapht.graph.Pseudograph;
Expand Down Expand Up @@ -80,12 +79,20 @@ public class LfNetwork extends AbstractPropertyBag implements PropertyBag {

private GraphDecrementalConnectivity<LfBus, LfBranch> connectivity;

private final Reporter reporter;

public LfNetwork(int numCC, int numSC, SlackBusSelector slackBusSelector,
GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory) {
GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory, Reporter reporter) {
this.numCC = numCC;
this.numSC = numSC;
this.slackBusSelector = Objects.requireNonNull(slackBusSelector);
this.connectivityFactory = Objects.requireNonNull(connectivityFactory);
this.reporter = Objects.requireNonNull(reporter);
}

public LfNetwork(int numCC, int numSC, SlackBusSelector slackBusSelector,
GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory) {
this(numCC, numSC, slackBusSelector, connectivityFactory, Reporter.NO_OP);
}

public int getNumCC() {
Expand All @@ -96,6 +103,10 @@ public int getNumSC() {
return numSC;
}

public Reporter getReporter() {
return reporter;
}

private void invalidateSlack() {
slackBus = null;
}
Expand Down Expand Up @@ -416,14 +427,7 @@ public void writeJson(Writer writer) {
}

private void reportSize(Reporter reporter) {
reporter.report(Report.builder()
.withKey("networkSize")
.withDefaultMessage("Network CC${numNetworkCc} SC${numNetworkSc} has ${busCount} buses and ${branchCount} branches")
.withValue("numNetworkCc", numCC)
.withValue("numNetworkSc", numSC)
.withValue("busCount", busesById.values().size())
.withValue("branchCount", branches.size())
.build());
Reports.reportNetworkSize(reporter, numCC, numSC, busesById.values().size(), branches.size());
LOGGER.info("Network {} has {} buses and {} branches",
this, busesById.values().size(), branches.size());
}
Expand All @@ -440,16 +444,7 @@ public void reportBalance(Reporter reporter) {
reactiveLoad += b.getLoadTargetQ() * PerUnit.SB;
}

reporter.report(Report.builder()
.withKey("networkBalance")
.withDefaultMessage("Network CC${numNetworkCc} SC${numNetworkSc} balance: active generation=${activeGeneration} MW, active load=${activeLoad} MW, reactive generation=${reactiveGeneration} MVar, reactive load=${reactiveLoad} MVar")
.withValue("numNetworkCc", numCC)
.withValue("numNetworkSc", numSC)
.withValue("activeGeneration", activeGeneration)
.withValue("activeLoad", activeLoad)
.withValue("reactiveGeneration", reactiveGeneration)
.withValue("reactiveLoad", reactiveLoad)
.build());
Reports.reportNetworkBalance(reporter, numCC, numSC, activeGeneration, activeLoad, reactiveGeneration, reactiveLoad);
LOGGER.info("Network {} balance: active generation={} MW, active load={} MW, reactive generation={} MVar, reactive load={} MVar",
this, activeGeneration, activeLoad, reactiveGeneration, reactiveLoad);
}
Expand All @@ -473,12 +468,7 @@ private void validateBuses(boolean dc, Reporter reporter) {
}
if (!hasAtLeastOneBusVoltageControlled) {
LOGGER.error("Network {} must have at least one bus voltage controlled", this);
reporter.report(Report.builder()
.withKey("networkMustHaveAtLEastOneBusVoltageControlled")
.withDefaultMessage("Network CC${numNetworkCc} SC${numNetworkSc} must have at least one bus voltage controlled")
.withValue("numNetworkCc", numCC)
.withValue("numNetworkSc", numSC)
.build());
Reports.reportNetworkMustHaveAtLeastOneBusVoltageControlled(reporter, numCC, numSC);
valid = false;
}
}
Expand Down Expand Up @@ -532,9 +522,7 @@ public static <T> List<LfNetwork> load(T network, LfNetworkLoader<T> networkLoad
Objects.requireNonNull(parameters);
List<LfNetwork> lfNetworks = networkLoader.load(network, parameters, reporter);
for (LfNetwork lfNetwork : lfNetworks) {
Reporter reporterNetwork = reporter.createSubReporter("postLoading", "Post loading process on network CC${numNetworkCc} SC${numNetworkSc}",
Map.of("numNetworkCc", new TypedValue(lfNetwork.getNumCC(), TypedValue.UNTYPED),
"numNetworkSc", new TypedValue(lfNetwork.getNumSC(), TypedValue.UNTYPED)));
Reporter reporterNetwork = Reports.createPostLoadingProcessingReporter(lfNetwork.getReporter());
lfNetwork.fix(parameters.isMinImpedance(), parameters.isDc());
lfNetwork.validate(parameters.isMinImpedance(), parameters.isDc(), reporterNetwork);
if (lfNetwork.isValid()) {
Expand Down
Loading

0 comments on commit 9153bfb

Please sign in to comment.