开式网络潮流计算编程c++代码
时间: 2023-07-23 12:42:51 浏览: 56
以下是一个简单的C++代码实现开式网络潮流计算的过程:
```c++
#include <iostream>
#include <cmath>
#define PI 3.141592654
using namespace std;
// 定义节点结构体
struct Node {
int id; // 节点编号
double volt; // 节点电压
double angle; // 节点相角
};
// 定义支路结构体
struct Branch {
int from; // 起始节点编号
int to; // 终止节点编号
double r; // 支路电阻
double x; // 支路电抗
double b; // 支路电纳
double tap; // 变压器变比
double angle; // 支路相角
};
// 计算支路导纳
complex<double> calcY(Branch branch) {
double r = branch.r;
double x = branch.x;
double b = branch.b;
double tap = branch.tap;
double a = cos(branch.angle * PI / 180) * tap;
double b = sin(branch.angle * PI / 180) * tap;
complex<double> z(r, x);
complex<double> y = 1.0 / z;
complex<double> ysh(0, b / 2.0);
y = y + ysh;
y = y / complex<double>(a, b);
return y;
}
// 计算节点注入功率
complex<double> calcS(Node node) {
double volt = node.volt;
double angle = node.angle;
complex<double> s(volt * cos(angle * PI / 180), volt * sin(angle * PI / 180));
return s;
}
// 计算节点电流注入
complex<double> calcI(Node node, Branch branch, complex<double> y) {
int id = node.id;
int from = branch.from;
int to = branch.to;
complex<double> s = calcS(node);
complex<double> v1 = complex<double>(node.volt * cos(node.angle * PI / 180), node.volt * sin(node.angle * PI / 180));
complex<double> v2 = complex<double>(0, 0);
if (id == from) {
v2 = complex<double>(node.volt * cos(branch.angle * PI / 180), node.volt * sin(branch.angle * PI / 180));
} else if (id == to) {
v2 = complex<double>(node.volt * cos(branch.angle * PI / 180 + branch.tap), node.volt * sin(branch.angle * PI / 180 + branch.tap));
}
complex<double> i = conj((v1 - v2) * y - s);
return i;
}
// 计算节点潮流
void calcPowerFlow(Node nodes[], Branch branches[], int n, int m) {
complex<double> ybus[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ybus[i][j] = complex<double>(0, 0);
}
}
for (int i = 0; i < m; i++) {
Branch branch = branches[i];
complex<double> y = calcY(branch);
ybus[branch.from][branch.from] += y;
ybus[branch.from][branch.to] -= y;
ybus[branch.to][branch.from] -= y;
ybus[branch.to][branch.to] += y;
}
for (int i = 0; i < n; i++) {
Node node = nodes[i];
complex<double> i_sum(0, 0);
for (int j = 0; j < n; j++) {
if (i != j) {
i_sum += calcI(node, branches[j], ybus[i][j]);
}
}
node.volt = abs(calcS(node) + i_sum) / abs(calcY(Branch{0, 0, 0, 0, 0, 1, 0}));
node.angle = arg(calcS(node) + i_sum) - arg(calcY(Branch{0, 0, 0, 0, 0, 1, 0}));
}
// 输出节点电压和相角
for (int i = 0; i < n; i++) {
cout << "Node " << nodes[i].id << ":\t" << nodes[i].volt << " V\t" << nodes[i].angle << " deg" << endl;
}
}
int main() {
int n = 3; // 节点数
int m = 3; // 支路数
// 定义节点和支路
Node nodes[n] = {{1, 1.0, 0.0}, {2, 1.0, 0.0}, {3, 1.0, 0.0}};
Branch branches[m] = {{1, 2, 0.1, 0.2, 0.04, 1.0, 0.0}, {1, 3, 0.05, 0.1, 0.02, 1.0, 0.0}, {2, 3, 0.05, 0.1, 0.02, 1.0, 0.0}};
// 计算潮流
calcPowerFlow(nodes, branches, n, m);
return 0;
}
```
在这个例子中,我们假设了一个3节点电力系统,计算了节点的电压和相角。函数`calcPowerFlow`实现了开式网络潮流计算的过程,其中使用了复数计算。我们定义了节点结构体和支路结构体,分别表示节点和支路的信息。函数`calcY`计算支路的导纳,函数`calcS`计算节点的注入功率,函数`calcI`计算节点的电流注入。在`calcPowerFlow`函数中,我们首先计算了节点导纳矩阵,然后使用迭代法计算了节点电压和相角。最后输出了节点的电压和相角。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)