-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_flow.hpp
334 lines (319 loc) · 9.77 KB
/
load_flow.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef LOAD_FLOW_GAUSS_SEIDEL
#define LOAD_FLOW_GAUSS_SEIDEL
#include <vector>
#include <complex>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#define width 10
#define YbusWidth 22 // Ybus values have kind of larger width
#define PI 3.14159265358979323846
using namespace std;
class load_flow
{
private:
int iterations, N;
string header, busInputData, lineInputData; //size of busData
vector<vector<double>> busData, lineData; // 0 base indexing
vector<complex<double>> S; // complex power , 1 base indexing
vector<vector<complex<double>>> V; // voltages , 1 base indexing
vector<vector<complex<double>>> Y;
void read();
void init_S(); //initialising S // busdata, S, N
void init_V(); // initialising V // busData, V, N
auto guass_seidel(int, int, bool);
void update();
void output();
auto reactive_power(int, int);
auto real_power(int, int);
public:
load_flow(string, string, int);
void calculateYBus();
void printBusData();
void printLineData();
void printYbus();
void solveLoadFlow();
};
/*
bus input data format:
type of bus, V, δ, Pg, Qg, Pl ,Ql ,Qg_max, Qg_min
type of bus is an integer 1-3
1: slack (there can be only one slack bus)
2: PV / Gen bus
3: PQ / Load bus
line input data format:
from bus, to bus, R, X,G,B, Max MVA
Assumptions:
* Bus1 is slack bus
* G=0;
*/
load_flow::load_flow(string busInputData, string lineInputData, int iter)
{
this->iterations = iter;
this->busInputData = busInputData;
this->lineInputData = lineInputData;
this->read();
this->N = this->busData.size();
this->init_S();
this->init_V();
}
void load_flow::read()
{
string line, word;
vector<double> row;
fstream fin;
fin.open(this->busInputData, ios::in);
if (!fin.is_open())
{
cout << "Bus input data file does not exist. Remember to add file name with extension\n";
exit(0);
}
else
{
getline(fin, line, '\n');
this->header = line;
int j = 0;
while (getline(fin, line, '\n'))
{
stringstream s(line);
while (getline(s, word, ','))
{
if (word == "")
row.push_back(0);
else
row.push_back(stod(word));
}
this->busData.push_back(row);
j++;
row.clear();
}
fin.close();
}
fin.open(this->lineInputData, ios::in);
if (!fin.is_open())
{
cout << "line input data file does not exist. Remember to add file name with extension\n";
exit(0);
}
else
{
getline(fin, line, '\n');
int i = 0;
while (getline(fin, line, '\n'))
{
stringstream s(line);
while (getline(s, word, ','))
{
if (i == 4) // assuming G= 0 hence not pushing it into LineData
{
i++;
continue;
}
row.push_back(stod(word));
i++;
}
i = 0;
this->lineData.push_back(row);
row.clear();
}
fin.close();
}
}
void load_flow::init_S()
{
S = vector<complex<double>>(N + 1, 0);
for (int i = 0; i < this->N; i++)
{
switch ((int)busData[i][0])
{
case 2: // PV Bus
this->S[i + 1].real(this->busData[i][3] - this->busData[i][5]);
break;
case 3: // PQ Bus
this->S[i + 1].real(this->busData[i][3] - this->busData[i][5]);
this->S[i + 1].imag(this->busData[i][4] - this->busData[i][6]);
break;
default:
break;
}
}
}
/* prints line data */
void load_flow::printLineData()
{
for (int i = 0; i < this->lineData.size(); i++)
{
for (int j = 0; j < this->lineData[i].size(); j++)
cout << left << setw(width) << setfill(' ') << this->lineData[i][j];
cout << "\n";
}
}
void load_flow::init_V()
{
V = vector<vector<complex<double>>>(N + 1);
this->V[1].push_back(complex<double>(1, 0));
for (int i = 1; i < this->N; i++)
{
switch ((int)this->busData[i][0])
{
case 2: // PV BUS
this->V[i + 1].push_back(complex<double>(this->busData[i][1], 0));
break;
case 3: // PQ BUS
this->V[i + 1].push_back(complex<double>(1, 0));
break;
default:
break;
}
}
}
/* prints bus data. data is updated after calling solveLoadFlow() */
void load_flow::printBusData()
{
for (int i = 0; i < this->N; i++)
{
cout << "bus " << i + 1 << " ";
for (int j = 1; j < this->busData[i].size(); j++)
cout << left << setw(width) << setfill(' ') << this->busData[i][j];
cout << "\n";
}
}
/*calculates the Y Bus matrix */
void load_flow::calculateYBus()
{
Y = vector<vector<complex<double>>>(N + 1, vector<complex<double>>(N + 1, 0));
complex<double> y1, y2, one(1, 0);
y2.real(0);
for (int i = 0; i < this->lineData.size(); i++)
{
y1.real(this->lineData[i][2]);
y1.imag(this->lineData[i][3]);
y1 = one / y1;
y2.imag(this->lineData[i][4] / 2);
this->Y[this->lineData[i][0]][this->lineData[i][1]] -= y1;
this->Y[this->lineData[i][1]][this->lineData[i][0]] -= y1;
this->Y[this->lineData[i][0]][this->lineData[i][0]] += (y1 + y2);
this->Y[this->lineData[i][1]][this->lineData[i][1]] += (y1 + y2);
}
}
/* Print the Y Bus matrix */
void load_flow::printYbus()
{
for (int i = 1; i <= this->N; i++)
{
for (int j = 1; j <= this->N; j++)
cout << left << setw(YbusWidth) << setfill(' ') << this->Y[i][j];
cout << "\n";
}
}
auto load_flow::guass_seidel(int itr, int k, bool flag)
{
complex<double> sum(0, 0);
for (int n = 1; n <= this->N; n++)
{
if (n < k)
sum += this->Y[k][n] * this->V[n][itr];
else if (n > k)
sum += this->Y[k][n] * this->V[n][itr - 1];
}
if (!flag) // the above equation can be applied two times in a single iteration (using V[k][itr-1] & then using V[k][itr])
return (conj(this->S[k] / this->V[k][itr - 1]) - sum) / (this->Y[k][k]);
return (conj(this->S[k] / this->V[k][itr]) - sum) / (this->Y[k][k]);
}
auto load_flow::reactive_power(int itr, int k)
{
double mag{abs(this->V[k][itr])};
double angle{arg(this->V[k][itr])};
double sum{0};
for (int n = 1; n <= this->N; n++)
sum += abs(this->V[n][itr]) * (this->Y[k][n].real() * sin(angle - arg(this->V[n][itr])) - this->Y[k][n].imag() * cos(angle - arg(this->V[n][itr])));
sum *= mag;
return sum;
}
auto load_flow::real_power(int itr, int k)
{
double mag{abs(this->V[k][itr])};
double angle{arg(this->V[k][itr])};
double sum{0};
for (int n = 1; n <= this->N; n++)
sum += abs(this->V[n][itr]) * (this->Y[k][n].real() * cos(angle - arg(this->V[n][itr])) + this->Y[k][n].imag() * sin(angle - arg(this->V[n][itr])));
sum *= mag;
return sum;
}
void load_flow::update()
{
for (int i = 1; i <= this->N; i++)
{
this->busData[i - 1][1] = abs(this->V[i][this->iterations]); // V
this->busData[i - 1][2] = arg(this->V[i][this->iterations]) * 180 / PI; // delta in degrees
this->busData[i - 1][3] = this->S[i].real() + this->busData[i - 1][5]; // Pg
this->busData[i - 1][4] = this->S[i].imag() + this->busData[i - 1][6]; // Qg
}
}
void load_flow::output()
{
fstream fout;
fout.open("output.csv", ios::out);
fout << this->header << "\n";
for (int i = 0; i < this->N; i++)
{
for (int j = 0; j < this->busData[i].size(); j++)
fout << this->busData[i][j] << ",";
fout << "\n";
}
}
/* solves the load flow problem using gauss-seidel iterative method.
updates the bus data.
final result is stored in output.csv
*/
void load_flow::solveLoadFlow()
{
bool type_change;
double Q, Qg;
// logic
for (int i = 1; i <= this->iterations; i++)
{
this->V[1].push_back(complex<double>(1, 0));
for (int j = 2; j <= this->N; j++)
{
switch ((int)this->busData[j - 1][0])
{
case 2: // pv // Load
type_change = true;
Q = this->reactive_power(i - 1, j);
Qg = Q + this->busData[j - 1][6]; // Qg = Q + Ql
if (Qg > this->busData[j - 1][7]) // Qg > Qgmax
Q = this->busData[j - 1][7];
else if (Qg < this->busData[j - 1][8]) // Qg < Qgmin
Q = this->busData[j - 1][8];
else
type_change = false;
this->S[j].imag(Q);
this->V[j].push_back(this->guass_seidel(i, j, 0));
if (!type_change) // Qg does not exceed its limits
{
this->V[j][i] = polar(abs(this->V[j][0]), arg(this->V[j][i]));
this->V[j][i] = this->guass_seidel(i, j, 1);
this->V[j][i] = polar(abs(this->V[j][0]), arg(this->V[j][i]));
}
else // Qg exceeded its limits
this->V[j][i] = this->guass_seidel(i, j, 1);
break;
case 3: // pq // Gen
this->V[j].push_back(this->guass_seidel(i, j, 0));
this->V[j][i] = this->guass_seidel(i, j, 1);
break;
default:
break;
}
}
}
// P and Q for slack bus
this->S[1].real(this->real_power(this->iterations, 1));
this->S[1].imag(this->reactive_power(this->iterations, 1));
// end of logic
this->update(); // updating busData
this->output();
}
#endif