TestSensitivityProgram.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package holeg.test_sensitivity;
  2. import holeg.power_flow.*;
  3. import java.io.*;
  4. import java.nio.charset.StandardCharsets;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Date;
  9. import java.util.List;
  10. public class TestSensitivityProgram {
  11. public static void test() {
  12. // Load data
  13. Bus[] buses = TestData.getIEEE14Busses();
  14. Line[] lines = TestData.getIEEE14Lines();
  15. float xStart = 0;
  16. float xEnd = 20.0f;
  17. float yStart = 0;
  18. float yEnd = 20.0f;
  19. float h = 0.1f;
  20. int[] lineIndex = new int[] { 6 };
  21. int busIndex = 12;
  22. List<String> csvOutput = new ArrayList<>();
  23. StringBuilder xLine = new StringBuilder();
  24. xLine.append(";");
  25. for (float x = xStart; x <= xEnd; x += h) {
  26. xLine.append(String.format("%.3f", x));
  27. xLine.append(";");
  28. }
  29. csvOutput.add(xLine.toString());
  30. double[] R = new double[lineIndex.length];
  31. double[] X = new double[lineIndex.length];
  32. for (int i = 0; i < lineIndex.length; i++) {
  33. R[i] = lines[lineIndex[i]].R;
  34. X[i] = lines[lineIndex[i]].X;
  35. }
  36. double Pg = buses[busIndex].Pg;
  37. double Pl = buses[busIndex].Pl;
  38. double Qg = buses[busIndex].Qg;
  39. double Ql = buses[busIndex].Ql;
  40. for (float y = yStart; y <= yEnd; y += h) {
  41. System.out.printf("%f / %f\n", y, yEnd);
  42. StringBuilder csvLine = new StringBuilder();
  43. csvLine.append(String.format("%.3f", y));
  44. csvLine.append(";");
  45. for (float x = xStart; x <= xEnd; x += h) {
  46. /*for (int i = 0; i <lineIndex.length; i++) {
  47. Line line = lines[lineIndex[i]];
  48. line.R = R[i] * (1 + x);
  49. line.X = X[i] * (1 + y);
  50. }*/
  51. /*Line lineA = lines[lineIndex[0]];
  52. lineA.R = R[0] * (1 + x);
  53. Line lineB = lines[lineIndex[1]];
  54. lineB.R = R[1] * (1 + y);*/
  55. /*Bus bus = buses[busIndex];
  56. bus.Pg = Pg * (1 + x);
  57. bus.Qg = Qg * (1 + y);*/
  58. Bus bus = buses[busIndex];
  59. bus.Pl = Pl * (1 + x);
  60. bus.Ql = Ql * (1 + y);
  61. // Create problem and solver
  62. PowerFlowProblem problem = new PowerFlowProblem(buses, lines, 1, 1 / 100.0);
  63. Solver solver = new NewtonRaphsonSolver();
  64. // Solve the problem with default settings
  65. SolverResult result = solver.solve(problem, new SolverSettings());
  66. if (result.solved && Arrays.stream(result.voltages).allMatch(v -> v.real > 0.3))
  67. csvLine.append(1);
  68. else
  69. csvLine.append(0);
  70. csvLine.append(";");
  71. }
  72. csvOutput.add(csvLine.toString());
  73. }
  74. String d = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date());
  75. writeToFile("C:\\Users\\hkunz\\Desktop\\test" + d + ".csv", csvOutput);
  76. System.out.println("Done!");
  77. }
  78. private static void writeToFile(String fileName, List<String> data) {
  79. PrintWriter out = null;
  80. try {
  81. out = new PrintWriter(new OutputStreamWriter(
  82. new BufferedOutputStream(new FileOutputStream(fileName)), StandardCharsets.UTF_8));
  83. for (String line : data)
  84. out.println(line);
  85. } catch (FileNotFoundException e) {
  86. e.printStackTrace();
  87. } finally {
  88. if (out != null) {
  89. out.flush();
  90. out.close();
  91. }
  92. }
  93. }
  94. }