SolverResult.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package holeg.power_flow;
  2. import java.io.PrintStream;
  3. public class SolverResult {
  4. public final boolean solved;
  5. public final SolverError error;
  6. public final SolverPerformance performance;
  7. public final ComplexNumber[] voltages;
  8. public final FlowData flowData;
  9. private SolverResult(boolean solved, SolverError error, SolverPerformance performance, ComplexNumber[] voltages, FlowData flowData) {
  10. this.solved = solved;
  11. this.error = error;
  12. this.performance = performance;
  13. this.voltages = voltages;
  14. this.flowData = flowData;
  15. }
  16. public static SolverResult success(SolverPerformance performance, ComplexNumber[] voltages, FlowData flowData) {
  17. if (performance == null)
  18. throw new IllegalArgumentException("performance is null");
  19. if (voltages == null)
  20. throw new IllegalArgumentException("voltages is null");
  21. if (flowData == null)
  22. throw new IllegalArgumentException("flowData is null");
  23. return new SolverResult(true, SolverError.None, performance, voltages, flowData);
  24. }
  25. public static SolverResult error(SolverError error) {
  26. if (error == SolverError.None)
  27. return error(SolverError.Internal);
  28. return new SolverResult(false, error, null,null, null);
  29. }
  30. public void printTo(PowerFlowProblem problem, PrintStream stream) {
  31. if (problem == null)
  32. throw new IllegalArgumentException("problem is null");
  33. if (stream == null)
  34. throw new IllegalArgumentException("stream is null");
  35. // Print error message
  36. if (!solved) {
  37. stream.println("Solver could not solve problem: " + error.name());
  38. return;
  39. }
  40. // Print data
  41. stream.printf("Solver solved problem! Took %d iterations in %.2f ms", performance.iterations, performance.timeInMilliseconds);
  42. stream.println();
  43. stream.printf("Error: %f", performance.error);
  44. stream.println();
  45. // Buses
  46. for (int i = 0; i < voltages.length; i++) {
  47. stream.printf("[Bus %2d %s] Voltage: %5.4f Vpu (%4.2f°), Power: %4.2f MW",
  48. i,
  49. problem.buses[i].type.name(),
  50. voltages[i].len(),
  51. Math.toDegrees(voltages[i].angle()),
  52. flowData.busInjection[i].real / problem.scalePower);
  53. stream.println();
  54. }
  55. // Lines
  56. for (int i = 0; i < problem.lines.length; i++) {
  57. Line line = problem.lines[i];
  58. stream.printf("[Line %2d -> %2d] Power: %5.3f MW, %5.3f Mvar, Loss: %5.3f MW, %5.3f Mvar",
  59. line.from,
  60. line.to,
  61. flowData.linePower[i].real / problem.scalePower,
  62. flowData.linePower[i].imaginary / problem.scalePower,
  63. flowData.linePowerLoss[i].real / problem.scalePower,
  64. flowData.linePowerLoss[i].imaginary / problem.scalePower);
  65. stream.println();
  66. }
  67. }
  68. }