123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package holeg.test_sensitivity;
- import holeg.power_flow.*;
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- public class TestSensitivityProgram {
- public static void test() {
- // Load data
- Bus[] buses = TestData.getIEEE14Busses();
- Line[] lines = TestData.getIEEE14Lines();
- float xStart = 0;
- float xEnd = 20.0f;
- float yStart = 0;
- float yEnd = 20.0f;
- float h = 0.1f;
- int[] lineIndex = new int[] { 6 };
- int busIndex = 12;
- List<String> csvOutput = new ArrayList<>();
- StringBuilder xLine = new StringBuilder();
- xLine.append(";");
- for (float x = xStart; x <= xEnd; x += h) {
- xLine.append(String.format("%.3f", x));
- xLine.append(";");
- }
- csvOutput.add(xLine.toString());
- double[] R = new double[lineIndex.length];
- double[] X = new double[lineIndex.length];
- for (int i = 0; i < lineIndex.length; i++) {
- R[i] = lines[lineIndex[i]].R;
- X[i] = lines[lineIndex[i]].X;
- }
- double Pg = buses[busIndex].Pg;
- double Pl = buses[busIndex].Pl;
- double Qg = buses[busIndex].Qg;
- double Ql = buses[busIndex].Ql;
- for (float y = yStart; y <= yEnd; y += h) {
- System.out.printf("%f / %f\n", y, yEnd);
- StringBuilder csvLine = new StringBuilder();
- csvLine.append(String.format("%.3f", y));
- csvLine.append(";");
- for (float x = xStart; x <= xEnd; x += h) {
- /*for (int i = 0; i <lineIndex.length; i++) {
- Line line = lines[lineIndex[i]];
- line.R = R[i] * (1 + x);
- line.X = X[i] * (1 + y);
- }*/
- /*Line lineA = lines[lineIndex[0]];
- lineA.R = R[0] * (1 + x);
- Line lineB = lines[lineIndex[1]];
- lineB.R = R[1] * (1 + y);*/
- /*Bus bus = buses[busIndex];
- bus.Pg = Pg * (1 + x);
- bus.Qg = Qg * (1 + y);*/
- Bus bus = buses[busIndex];
- bus.Pl = Pl * (1 + x);
- bus.Ql = Ql * (1 + y);
- // Create problem and solver
- PowerFlowProblem problem = new PowerFlowProblem(buses, lines, 1, 1 / 100.0);
- Solver solver = new NewtonRaphsonSolver();
- // Solve the problem with default settings
- SolverResult result = solver.solve(problem, new SolverSettings());
- if (result.solved && Arrays.stream(result.voltages).allMatch(v -> v.real > 0.3))
- csvLine.append(1);
- else
- csvLine.append(0);
- csvLine.append(";");
- }
- csvOutput.add(csvLine.toString());
- }
- String d = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date());
- writeToFile("C:\\Users\\hkunz\\Desktop\\test" + d + ".csv", csvOutput);
- System.out.println("Done!");
- }
- private static void writeToFile(String fileName, List<String> data) {
- PrintWriter out = null;
- try {
- out = new PrintWriter(new OutputStreamWriter(
- new BufferedOutputStream(new FileOutputStream(fileName)), StandardCharsets.UTF_8));
- for (String line : data)
- out.println(line);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- if (out != null) {
- out.flush();
- out.close();
- }
- }
- }
- }
|