-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
220 lines (181 loc) · 6.65 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <queue>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
class Component {
public:
string name;
string type;
int node1, node2;
double value;
Component(string n, string t, int n1, int n2, double v)
: name(n), type(t), node1(n1), node2(n2), value(v) {}
};
class Circuit {
private:
vector<Component> components;
map<int, int> nodeMap;
map<int, vector<int>> adjacencyList;
int numNodes;
int numVoltSources;
bool isConnectedToGround(int node, set<int>& visited) {
if (node == 0) return true; // Ground node
visited.insert(node);
for (int neighbor : adjacencyList[node]) {
if (visited.find(neighbor) == visited.end()) {
if (isConnectedToGround(neighbor, visited)) {
return true;
}
}
}
return false;
}
void buildAdjacencyList() {
adjacencyList.clear();
for (const Component& comp : components) {
adjacencyList[comp.node1].push_back(comp.node2);
adjacencyList[comp.node2].push_back(comp.node1);
}
}
void checkCircuitTopology() {
buildAdjacencyList();
// Check if each node is connected to ground
for (const auto& node : nodeMap) {
if (node.first == 0) continue; // Skip ground node
set<int> visited;
if (!isConnectedToGround(node.first, visited)) {
throw runtime_error("Node " + to_string(node.first) + " is floating (not connected to ground)");
}
}
// Check for voltage source loops
vector<bool> hasVoltageConnection(numNodes, false);
for (const Component& comp : components) {
if (comp.type == "V") {
if (hasVoltageConnection[nodeMap[comp.node1]] &&
hasVoltageConnection[nodeMap[comp.node2]]) {
throw runtime_error("Invalid voltage source configuration detected");
}
hasVoltageConnection[nodeMap[comp.node1]] = true;
hasVoltageConnection[nodeMap[comp.node2]] = true;
}
}
}
public:
Circuit() : numNodes(0), numVoltSources(0) {}
void parseNetlist(const string& filename) {
ifstream file(filename);
if (!file.is_open()) {
throw runtime_error("Could not open file: " + filename);
}
string line;
components.clear();
nodeMap.clear();
numNodes = 0;
numVoltSources = 0;
// Always add ground node (0) first
nodeMap[0] = numNodes++;
while (getline(file, line)) {
if (line.empty() || line[0] == '*') continue;
istringstream iss(line);
string name, node1Str, node2Str, valueStr;
if (!(iss >> name >> node1Str >> node2Str >> valueStr)) {
throw runtime_error("Invalid line format: " + line);
}
try {
int node1 = stoi(node1Str);
int node2 = stoi(node2Str);
double value = stod(valueStr);
// Validate node numbers
if (node1 < 0 || node2 < 0) {
throw runtime_error("Node numbers must be non-negative");
}
if (nodeMap.find(node1) == nodeMap.end())
nodeMap[node1] = numNodes++;
if (nodeMap.find(node2) == nodeMap.end())
nodeMap[node2] = numNodes++;
string type = string(1, name[0]);
if (type != "R" && type != "V") {
throw runtime_error("Unsupported component type: " + type);
}
if (value <= 0) {
throw runtime_error("Component values must be positive");
}
components.emplace_back(name, type, node1, node2, value);
if (type == "V") numVoltSources++;
}
catch (const exception& e) {
throw runtime_error("Error parsing line: " + line + "\nError: " + e.what());
}
}
cout << "Circuit parsed successfully:\n";
cout << "Number of nodes: " << numNodes << "\n";
cout << "Number of voltage sources: " << numVoltSources << "\n";
cout << "Number of components: " << components.size() << "\n\n";
// Verify circuit topology
checkCircuitTopology();
}
void solveCircuit() {
if (components.empty()) {
throw runtime_error("No components in circuit");
}
int matrixSize = numNodes + numVoltSources;
cout << "Creating " << matrixSize << "x" << matrixSize << " matrix\n";
MatrixXd A = MatrixXd::Zero(matrixSize, matrixSize);
VectorXd b = VectorXd::Zero(matrixSize);
int voltSourceIndex = numNodes;
for (const Component& comp : components) {
int n1 = nodeMap[comp.node1];
int n2 = nodeMap[comp.node2];
if (comp.type == "R") {
double conductance = 1.0 / comp.value;
if (n1 != 0) {
A(n1, n1) += conductance;
if (n2 != 0) A(n1, n2) -= conductance;
}
if (n2 != 0) {
A(n2, n2) += conductance;
if (n1 != 0) A(n2, n1) -= conductance;
}
}
else if (comp.type == "V") {
if (n1 != 0) A(n1, voltSourceIndex) = 1;
if (n2 != 0) A(n2, voltSourceIndex) = -1;
if (n1 != 0) A(voltSourceIndex, n1) = 1;
if (n2 != 0) A(voltSourceIndex, n2) = -1;
b(voltSourceIndex) = comp.value;
voltSourceIndex++;
}
}
cout << "\nCircuit matrix A:\n" << A << "\n\n";
cout << "Vector b:\n" << b << "\n\n";
VectorXd x = A.colPivHouseholderQr().solve(b);
cout << "Node Voltages:\n";
for (const auto& node : nodeMap) {
if (node.first == 0) {
cout << "Node " << node.first << " (GND): 0.0V\n";
} else {
cout << "Node " << node.first << ": " << x(node.second) << "V\n";
}
}
}
};
int main() {
try {
Circuit circuit;
circuit.parseNetlist("D:/Dev/Projects/circuit-solver/circuit.txt"); // enter the absolute path to the circuit.txt file
circuit.solveCircuit();
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}