-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circuit.cpp
211 lines (194 loc) · 5.91 KB
/
Circuit.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
#include <algorithm>
#include "Circuit.h"
#include "NetlistParser.h"
namespace Tran {
static size_t
findDeviceId(size_t nodeId1, size_t nodeId2,
const std::vector<Node>& nodes)
{
const Node& node1 = nodes[nodeId1];
const Node& node2 = nodes[nodeId2];
std::vector<size_t> devs1 = node1._connection;
std::sort(devs1.begin(), devs1.end());
std::vector<size_t> devs2 = node2._connection;
std::sort(devs2.begin(), devs2.end());
for (size_t dev1 : devs1) {
for (size_t dev2 : devs2) {
if (dev1 == dev2) {
return dev1;
}
}
}
return static_cast<size_t>(-1);
}
typedef std::unordered_map<std::string, size_t> StringIdMap;
static void
incrCountMap(StringIdMap& countMap, const std::string& key)
{
auto it = countMap.find(key);
if (it == countMap.end()) {
countMap.insert({key, 1});
} else {
it->second += 1;
}
}
static std::string
findGroundNode(const std::vector<ParserDevice>& devs, std::vector<std::string>& allNodeNames)
{
StringIdMap nodeConnectionCount;
for (const ParserDevice& dev : devs) {
incrCountMap(nodeConnectionCount, dev._posNode);
incrCountMap(nodeConnectionCount, dev._negNode);
}
size_t maxCount = 0;
std::string maxNode;
for (const auto& kv : nodeConnectionCount) {
if (kv.second > maxCount ||
(kv.second == maxCount && kv.first.compare(maxNode) < 0)) {
maxCount = kv.second;
maxNode = kv.first;
}
allNodeNames.push_back(kv.first);
}
return maxNode;
}
static size_t
findNodeByName(const StringIdMap& nodeIdMap,
const std::string& name)
{
const auto& it = nodeIdMap.find(name);
if (it == nodeIdMap.end()) {
return static_cast<size_t>(-1);
}
return it->second;
}
bool
createDevice(Device& dev, const ParserDevice& pDev, const StringIdMap& nodeIdMap)
{
size_t posNode = findNodeByName(nodeIdMap, pDev._posNode);
size_t negNode = findNodeByName(nodeIdMap, pDev._negNode);
if (posNode == static_cast<size_t>(-1)) {
printf("Cannot find node \"%s\" referenced by device %s\n", pDev._posNode.data(), pDev._name.data());
}
if (negNode == static_cast<size_t>(-1)) {
printf("Cannot find node \"%s\" referenced by device %s\n", pDev._negNode.data(), pDev._name.data());
}
if (posNode == static_cast<size_t>(-1) || negNode == static_cast<size_t>(-1)) {
return false;
}
dev._posNode = posNode;
dev._negNode = negNode;
if (pDev._type == DeviceType::CCCS || pDev._type == DeviceType::CCVS ||
pDev._type == DeviceType::VCCS || pDev._type == DeviceType::VCVS) {
size_t posSampleNode = findNodeByName(nodeIdMap, pDev._posSampleNode);
size_t negSampleNode = findNodeByName(nodeIdMap, pDev._negSampleNode);
if (posSampleNode == static_cast<size_t>(-1)) {
printf("Cannot find node \"%s\" referenced by device %s\n", pDev._posNode.data(), pDev._name.data());
}
if (negSampleNode == static_cast<size_t>(-1)) {
printf("Cannot find node \"%s\" referenced by device %s\n", pDev._posNode.data(), pDev._name.data());
}
if (posSampleNode == static_cast<size_t>(-1) || negSampleNode == static_cast<size_t>(-1)) {
return false;
}
dev._posSampleNode = posSampleNode;
dev._negSampleNode = negSampleNode;
}
dev._name = pDev._name;
dev._type = pDev._type;
dev._isPWLValue = pDev._isPWLValue;
if (dev._isPWLValue) {
dev._PWLData = pDev._PWLData;
} else {
dev._value = pDev._value;
}
return true;
}
Circuit::Circuit(const NetlistParser& parser)
: _PWLData(parser.PWLData())
{
const std::vector<ParserDevice>& parserDevs = parser.devices();
std::vector<std::string> allNodeNames;
const std::string& groundNodeName = findGroundNode(parserDevs, allNodeNames);
std::unordered_map<std::string, size_t> nodeIdMap;
printf("Ground node identified as node \"%s\"\n", groundNodeName.data());
_nodes.reserve(allNodeNames.size());
Node ground;
ground._name = groundNodeName;
ground._nodeId = 0;
ground._isGround = true;
_nodes.push_back(ground);
nodeIdMap.insert({ground._name, ground._nodeId});
_groundNodeId = ground._nodeId;
for (const std::string& nodeName : allNodeNames) {
if (nodeName == groundNodeName) {
continue;
}
Node n;
n._name = nodeName;
n._nodeId = _nodes.size();
n._isGround = false;
_nodes.push_back(n);
nodeIdMap.insert({n._name, n._nodeId});
}
_devices.reserve(parserDevs.size());
for (const ParserDevice& pDev : parserDevs) {
Device dev;
size_t devId = _devices.size();
dev._devId = devId;
if (createDevice(dev, pDev, nodeIdMap) == false) {
continue;
}
_devices.push_back(dev);
size_t posNode = dev._posNode;
_nodes[posNode]._connection.push_back(devId);
size_t negNode = dev._negNode;
_nodes[negNode]._connection.push_back(devId);
}
for (size_t i=0; i<_devices.size(); ++i) {
Device& dev = _devices[i];
if (dev._type == DeviceType::CCCS ||
dev._type == DeviceType::CCVS) {
size_t sampleDev = findDeviceId(dev._posSampleNode, dev._negSampleNode, _nodes);
if (sampleDev == static_cast<size_t>(-1)) {
printf("ERROR: Cannot find sampling branch with %s and %s of current controlled device %s\n",
_nodes[dev._posSampleNode]._name.data(), _nodes[dev._negSampleNode]._name.data(), dev._name.data());
} else {
dev._sampleDevice = sampleDev;
}
}
}
}
const PWLValue&
Circuit::PWLData(const Device& dev) const
{
static PWLValue empty;
if (dev._isPWLValue == false) {
return empty;
}
return _PWLData[dev._PWLData];
}
const Device&
Circuit::findDeviceByName(const std::string& name) const
{
for (const Device& dev : _devices) {
if (dev._name == name) {
return dev;
}
}
static Device empty;
empty._type = DeviceType::Total;
return empty;
}
const Node&
Circuit::findNodeByName(const std::string& name) const
{
for (const Node& node : _nodes) {
if (node._name == name) {
return node;
}
}
static Node empty;
return empty;
}
}