package holeg.power_flow; import java.io.PrintStream; public class SolverResult { public final boolean solved; public final SolverError error; public final SolverPerformance performance; public final ComplexNumber[] voltages; public final FlowData flowData; private SolverResult(boolean solved, SolverError error, SolverPerformance performance, ComplexNumber[] voltages, FlowData flowData) { this.solved = solved; this.error = error; this.performance = performance; this.voltages = voltages; this.flowData = flowData; } public static SolverResult success(SolverPerformance performance, ComplexNumber[] voltages, FlowData flowData) { if (performance == null) throw new IllegalArgumentException("performance is null"); if (voltages == null) throw new IllegalArgumentException("voltages is null"); if (flowData == null) throw new IllegalArgumentException("flowData is null"); return new SolverResult(true, SolverError.None, performance, voltages, flowData); } public static SolverResult error(SolverError error) { if (error == SolverError.None) return error(SolverError.Internal); return new SolverResult(false, error, null,null, null); } public void printTo(PowerFlowProblem problem, PrintStream stream) { if (problem == null) throw new IllegalArgumentException("problem is null"); if (stream == null) throw new IllegalArgumentException("stream is null"); // Print error message if (!solved) { stream.println("Solver could not solve problem: " + error.name()); return; } // Print data stream.printf("Solver solved problem! Took %d iterations in %.2f ms", performance.iterations, performance.timeInMilliseconds); stream.println(); stream.printf("Error: %f", performance.error); stream.println(); // Buses for (int i = 0; i < voltages.length; i++) { stream.printf("[Bus %2d %s] Voltage: %5.4f Vpu (%4.2f°), Power: %4.2f MW", i, problem.buses[i].type.name(), voltages[i].len(), Math.toDegrees(voltages[i].angle()), flowData.busInjection[i].real / problem.scalePower); stream.println(); } // Lines for (int i = 0; i < problem.lines.length; i++) { Line line = problem.lines[i]; stream.printf("[Line %2d -> %2d] Power: %5.3f MW, %5.3f Mvar, Loss: %5.3f MW, %5.3f Mvar", line.from, line.to, flowData.linePower[i].real / problem.scalePower, flowData.linePower[i].imaginary / problem.scalePower, flowData.linePowerLoss[i].real / problem.scalePower, flowData.linePowerLoss[i].imaginary / problem.scalePower); stream.println(); } } }