TestSensitivityProgram.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package holeg.test_sensitivity;
  2. import holeg.power_flow.*;
  3. import java.io.*;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class TestSensitivityProgram {
  8. public static void test() {
  9. // Load data
  10. Bus[] buses = TestData.getIEEE14Busses();
  11. Line[] lines = TestData.getIEEE14Lines();
  12. float xStart = -0.8f;
  13. float xEnd = 10.0f;
  14. float yStart = -0.8f;
  15. float yEnd = 10.0f;
  16. float h = 0.5f;
  17. int lineIndex = 7;
  18. int busIndex = 5;
  19. List<String> csvOutput = new ArrayList<>();
  20. StringBuilder xLine = new StringBuilder();
  21. xLine.append(";");
  22. for (float x = xStart; x <= xEnd; x += h) {
  23. xLine.append(String.format("%.3f", x));
  24. xLine.append(";");
  25. }
  26. csvOutput.add(xLine.toString());
  27. double R = lines[lineIndex].R;
  28. double X = lines[lineIndex].X;
  29. double Pg = buses[busIndex].Pg;
  30. double Pl = buses[busIndex].Pl;
  31. for (float y = yStart; y <= yEnd; y += h) {
  32. System.out.printf("%f / %f\n", y, yEnd);
  33. StringBuilder csvLine = new StringBuilder();
  34. csvLine.append(String.format("%.3f", y));
  35. csvLine.append(";");
  36. for (float x = xStart; x <= xEnd; x += h) {
  37. Line line = lines[lineIndex];
  38. line.R = R * (1 + x);
  39. line.X = X * (1 + y);
  40. /*Bus bus = buses[busIndex];
  41. bus.Pg = Pg * (1 + x);
  42. bus.Pl = Pl * (1 + y);*/
  43. // Create problem and solver
  44. PowerFlowProblem problem = new PowerFlowProblem(buses, lines, 1, 1 / 100.0);
  45. Solver solver = new NewtonRaphsonSolver();
  46. // Solve the problem with default settings
  47. SolverResult result = solver.solve(problem, new SolverSettings());
  48. if (result.solved)
  49. csvLine.append(1);
  50. else
  51. csvLine.append(0);
  52. csvLine.append(";");
  53. }
  54. csvOutput.add(csvLine.toString());
  55. }
  56. writeToFile("C:\\Users\\Henrik\\Desktop\\test.csv", csvOutput);
  57. System.out.println("Done!");
  58. }
  59. private static void writeToFile(String fileName, List<String> data) {
  60. PrintWriter out = null;
  61. try {
  62. out = new PrintWriter(new OutputStreamWriter(
  63. new BufferedOutputStream(new FileOutputStream(fileName)), StandardCharsets.UTF_8));
  64. for (String line : data)
  65. out.println(line);
  66. } catch (FileNotFoundException e) {
  67. e.printStackTrace();
  68. } finally {
  69. if (out != null) {
  70. out.flush();
  71. out.close();
  72. }
  73. }
  74. }
  75. }