-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeList.cpp
138 lines (121 loc) · 3.51 KB
/
NodeList.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
/*
* File: NodeList.cpp
* Author: Matthew
*
* Created on November 6, 2016, 5:50 PM
*/
#include "NodeList.h"
#define MIN_ITERATION_CHANGE 0.0001
NodeList::NodeList() {
setHead(NULL);
setTail(NULL);
}
NodeList::~NodeList(){
clear();
}
void NodeList::clear(){
delete head;
setHead(NULL);
setTail(NULL);
}
void NodeList::setHead(Node* n){
head = n;
}
void NodeList::setTail(Node* n){
tail = n;
}
Node* NodeList::findAddNode(int id){
if(head==NULL){
Node* n = new Node(id);
setHead(n);
setTail(n);
return head;
}
for(Node* cur = head; cur!=NULL; cur=cur->getNext()){
if(cur->getID()==id) return cur;
if(cur->getID()>id){ //n location is found
Node* n = new Node(id);
n->setPrev(cur->getPrev());
//cur->getPrev() would be NULL if cur is head
if(cur==head) head = n;
else (n->getPrev())->setNext(n);
n->setNext(cur);
cur->setPrev(n);
return n;
}
else if(cur==tail){ //n is the largest in the list
Node* n = new Node(id);
cur->setNext(n);
n->setPrev(cur);
setTail(n);
return n;
}
}
}
Node* NodeList::search(int id){
for(Node* cur = head; cur!=NULL; cur=cur->getNext())
if(cur->getID()==id) return cur;
return NULL;
}
double* NodeList::getOthersV(Node* cur){ //Returns voltage array
int* others = cur->getOtherNodes(); //List of nodes linked to cur
double* othersV;
int count = 0;
while(others[count]!=-1){
othersV[count] = search(others[count])->getV();
count++;
}
othersV[count] = -1;
return othersV;
}
void NodeList::solve(){
double minChange = 1;
double vi,vTemp;
double* othersV, *othersR;
double iSum,rSum; // V/R sum term, 1/(1/Ri) sum term
int count = 0;
for(Node* cur = head; cur!=NULL; cur=cur->getNext())
if(!cur->isSet()) cur->setV(0);
while((minChange>MIN_ITERATION_CHANGE)){
for(Node* cur = head; cur!=NULL; cur=cur->getNext()){
if(!cur->isSet()){
othersV = getOthersV(cur);
othersR = cur->getResistors();
rSum = cur->inverseSum();
vi = cur->getV();
iSum=0;
for(count = 0; othersV[count]!=-1; count++)
iSum+=(othersV[count]/othersR[count]);
vTemp = iSum*rSum;
cur->setV(vTemp);
vTemp-=vi;
if(vTemp<0) vTemp*=-1;
if(vTemp<minChange) minChange = vTemp;
}
}
}
cout << "Solve:" << endl;
for(Node* cur = head; cur!=NULL; cur=cur->getNext())
cout<<" Node "<<cur->getID()<<": "<<cur->getV()<<" V"<<endl;
}
bool NodeList::unknown(){
for(Node* cur = head; cur!=NULL; cur=cur->getNext())
if(cur->isSet()) return false;
return true;
}
Resistor* NodeList::getR(string name){
for(Node* cur = head; cur!=NULL; cur=cur->getNext()){
if(cur->getR(name)!=NULL)
return (cur->getR(name));
}
return NULL;
}
int* NodeList::getPoints(string rName){
Resistor* temp = getR(rName);
if(temp==NULL) return NULL;
else return temp->getPoints();
}
void NodeList::printAll(){
for(Node* cur = head; cur!=NULL; cur=cur->getNext())
cur->print();
}