diff --git a/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java b/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java index 814c28fd24..3f2c4817f6 100644 --- a/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java +++ b/src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java @@ -7,7 +7,6 @@ package com.powsybl.openloadflow; import com.google.auto.service.AutoService; -import com.google.common.collect.ImmutableMap; import com.powsybl.computation.ComputationManager; import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.extensions.SlackTerminal; @@ -41,7 +40,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; import java.util.concurrent.CompletableFuture; /** @@ -82,12 +84,6 @@ private static AcLoadFlowObserver getObserver(OpenLoadFlowParameters parametersE return AcLoadFlowObserver.of(observers); } - private static ImmutableMap createMetrics(AcLoadFlowResult result) { - String prefix = "network_" + result.getNetwork().getNum() + "_"; - return ImmutableMap.of(prefix + "iterations", Integer.toString(result.getNewtonRaphsonIterations()), - prefix + "status", result.getNewtonRaphsonStatus().name()); - } - private static VoltageInitializer getVoltageInitializer(LoadFlowParameters parameters) { switch (parameters.getVoltageInitMode()) { case UNIFORM_VALUES: @@ -175,15 +171,12 @@ private CompletableFuture runAc(Network network, String workingS SlackTerminal.reset(network); } - Map metrics = new HashMap<>(); List componentResults = new ArrayList<>(results.size()); for (AcLoadFlowResult result : results) { // update network state result.getNetwork().updateState(!parameters.isNoGeneratorReactiveLimits(), parameters.isWriteSlackBus(), parameters.isPhaseShifterRegulationOn(), parameters.isTransformerVoltageControlOn()); - metrics.putAll(createMetrics(result)); - LoadFlowResult.ComponentResult.Status status; switch (result.getNewtonRaphsonStatus()) { case CONVERGED: @@ -217,7 +210,7 @@ private CompletableFuture runAc(Network network, String workingS }).complete(); } - return new LoadFlowResultImpl(ok, metrics, null, componentResults); + return new LoadFlowResultImpl(ok, Collections.emptyMap(), null, componentResults); }); } @@ -235,14 +228,24 @@ private CompletableFuture runDc(Network network, String workingS Networks.resetState(network); - if (result.isOk() && parameters.isWriteSlackBus()) { + if (result.getStatus() == LoadFlowResult.ComponentResult.Status.CONVERGED && parameters.isWriteSlackBus()) { SlackTerminal.reset(network); } result.getNetwork().updateState(false, parameters.isWriteSlackBus(), parameters.isPhaseShifterRegulationOn(), parameters.isTransformerVoltageControlOn()); - return new LoadFlowResultImpl(result.isOk(), Collections.emptyMap(), null); + LoadFlowResult.ComponentResult componentResult = new LoadFlowResultImpl.ComponentResultImpl( + result.getNetwork().getNum(), + result.getStatus(), + 0, + result.getNetwork().getSlackBus().getId(), + result.getSlackBusActivePowerMismatch() * PerUnit.SB); + + return new LoadFlowResultImpl(result.getStatus() == LoadFlowResult.ComponentResult.Status.CONVERGED, + Collections.emptyMap(), + null, + Collections.singletonList(componentResult)); }); } diff --git a/src/main/java/com/powsybl/openloadflow/ac/nr/DcValueVoltageInitializer.java b/src/main/java/com/powsybl/openloadflow/ac/nr/DcValueVoltageInitializer.java index 3db4d979eb..9b956afe79 100644 --- a/src/main/java/com/powsybl/openloadflow/ac/nr/DcValueVoltageInitializer.java +++ b/src/main/java/com/powsybl/openloadflow/ac/nr/DcValueVoltageInitializer.java @@ -7,6 +7,7 @@ package com.powsybl.openloadflow.ac.nr; import com.powsybl.commons.PowsyblException; +import com.powsybl.loadflow.LoadFlowResult; import com.powsybl.math.matrix.MatrixFactory; import com.powsybl.openloadflow.dc.DcLoadFlowEngine; import com.powsybl.openloadflow.equations.VoltageInitializer; @@ -20,7 +21,7 @@ public class DcValueVoltageInitializer implements VoltageInitializer { @Override public void prepare(LfNetwork network, MatrixFactory matrixFactory) { - if (!new DcLoadFlowEngine(network, matrixFactory).run().isOk()) { + if (new DcLoadFlowEngine(network, matrixFactory).run().getStatus() != LoadFlowResult.ComponentResult.Status.CONVERGED) { throw new PowsyblException("DC loadflow failed, impossible to initialize voltage angle from DC values"); } } diff --git a/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java b/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java index 6a8f4ee29f..72d10f2f5d 100644 --- a/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java +++ b/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java @@ -8,6 +8,7 @@ import com.google.common.base.Stopwatch; import com.powsybl.loadflow.LoadFlowParameters; +import com.powsybl.loadflow.LoadFlowResult; import com.powsybl.math.matrix.LUDecomposition; import com.powsybl.math.matrix.MatrixFactory; import com.powsybl.openloadflow.dc.equations.DcEquationSystem; @@ -85,13 +86,13 @@ public DcLoadFlowResult run() { try { double[] dx = Arrays.copyOf(targets, targets.length); - boolean ok; + LoadFlowResult.ComponentResult.Status status; try { LUDecomposition lu = j.decomposeLU(); lu.solveTransposed(dx); - ok = true; + status = LoadFlowResult.ComponentResult.Status.CONVERGED; } catch (Exception e) { - ok = false; + status = LoadFlowResult.ComponentResult.Status.FAILED; LOGGER.error("Failed to solve linear system for DC load flow", e); } @@ -106,9 +107,9 @@ public DcLoadFlowResult run() { stopwatch.stop(); LOGGER.debug(Markers.PERFORMANCE_MARKER, "Dc loadflow ran in {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS)); - LOGGER.info("Dc loadflow complete (ok={})", ok); + LOGGER.info("Dc loadflow complete (status={})", status); - return new DcLoadFlowResult(network, ok); + return new DcLoadFlowResult(network, network.getActivePowerMismatch(), status); } finally { j.cleanLU(); } diff --git a/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowResult.java b/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowResult.java index 3ea58b87a9..64caa6ca77 100644 --- a/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowResult.java +++ b/src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowResult.java @@ -6,6 +6,7 @@ */ package com.powsybl.openloadflow.dc; +import com.powsybl.loadflow.LoadFlowResult; import com.powsybl.openloadflow.network.LfNetwork; import java.util.Objects; @@ -17,18 +18,25 @@ public class DcLoadFlowResult { private final LfNetwork network; - private final boolean ok; + private final double slackBusActivePowerMismatch; - public DcLoadFlowResult(LfNetwork network, boolean ok) { + private final LoadFlowResult.ComponentResult.Status status; + + public DcLoadFlowResult(LfNetwork network, double slackBusActivePowerMismatch, LoadFlowResult.ComponentResult.Status status) { this.network = Objects.requireNonNull(network); - this.ok = ok; + this.slackBusActivePowerMismatch = slackBusActivePowerMismatch; + this.status = status; } public LfNetwork getNetwork() { return network; } - public boolean isOk() { - return ok; + public double getSlackBusActivePowerMismatch() { + return slackBusActivePowerMismatch; + } + + public LoadFlowResult.ComponentResult.Status getStatus() { + return status; } } diff --git a/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowEurostagTutorialExample1Test.java b/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowEurostagTutorialExample1Test.java index 0c52fd3a7e..79afa3d700 100644 --- a/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowEurostagTutorialExample1Test.java +++ b/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowEurostagTutorialExample1Test.java @@ -74,7 +74,7 @@ void setUp() { void baseCaseTest() { LoadFlowResult result = loadFlowRunner.run(network, parameters); assertTrue(result.isOk()); - assertEquals("3", result.getMetrics().get("network_0_iterations")); + assertEquals(3, result.getComponentResults().get(0).getIterationCount()); assertVoltageEquals(24.5, genBus); assertAngleEquals(0, genBus); @@ -113,7 +113,7 @@ public void afterStateVectorCreation(double[] x, int iteration) { }); LoadFlowResult result = loadFlowRunner.run(network, parameters); assertTrue(result.isOk()); - assertEquals("3", result.getMetrics().get("network_0_iterations")); + assertEquals(3, result.getComponentResults().get(0).getIterationCount()); assertTrue(stateVectorInitialized[0]); } diff --git a/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowTwoBusNetworkTest.java b/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowTwoBusNetworkTest.java index 3b61cef112..dcd7084e9c 100644 --- a/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowTwoBusNetworkTest.java +++ b/src/test/java/com/powsybl/openloadflow/ac/AcLoadFlowTwoBusNetworkTest.java @@ -71,11 +71,11 @@ void baseCaseTest() { void voltageInitModeTest() { LoadFlowResult result = loadFlowRunner.run(network, parameters); assertTrue(result.isOk()); - assertEquals("3", result.getMetrics().get("network_0_iterations")); + assertEquals(3, result.getComponentResults().get(0).getIterationCount()); // restart loadflow from previous calculated state, it should convergence in zero iteration result = loadFlowRunner.run(network, parameters.setVoltageInitMode(LoadFlowParameters.VoltageInitMode.PREVIOUS_VALUES)); assertTrue(result.isOk()); - assertEquals("1", result.getMetrics().get("network_0_iterations")); + assertEquals(1, result.getComponentResults().get(0).getIterationCount()); } @Test