Skip to content

Commit 1718ca4

Browse files
committed
fix issue when a component is shorted, or multiple gnd are used
1 parent 8d448b1 commit 1718ca4

File tree

3 files changed

+83
-42
lines changed

3 files changed

+83
-42
lines changed

src/App.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,6 @@ function App() {
263263
</div>
264264
<div className="row shadow-sm rounded bg-lightgreen my-2 py-3" id="schematic">
265265
<ChoseTF
266-
// setTextResult={setTextResult}
267-
// setMathML={setMathML}
268-
// setComplexResponse={setComplexResponse}
269266
setResults={setResults}
270267
nodes={nodes}
271268
fullyConnectedComponents={fullyConnectedComponents}
@@ -288,13 +285,13 @@ function App() {
288285
</>
289286
)}
290287
</div>
291-
288+
<div key="releaseNotes" className="row my-2 py-1 shadow-sm rounded bg-lightgreen">
289+
<ReleaseNotes />
290+
</div>
292291
<div key="comments" className="row my-2 py-1 shadow-sm rounded bg-lightgreen">
293292
{!import.meta.env.DEV && <Comments website-id="12350" page-id="7" />}
294293
</div>
295294
</Container>
296-
<ReleaseNotes />
297-
{/* </div> */}
298295
</div>
299296
<Footer />
300297
</>

src/VisioJSSchematic.jsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ export function VisioJSSchematic({ setResults, setNodes, setComponentValues, set
100100

101101
//build the MNA matrix - do this after use clicks calculateMNA - FIXME
102102
// build_and_solve_mna(nodeMap, 'vin', addShapes )
103+
// console.log("nodemap", nodeMap);
104+
// console.log("fullyConnectedComponents", fullyConnectedComponents, components);
105+
103106
},
104107
[oldComponents, setComponentValues, setFullyConnectedComponents, setNodes, setResults],
105108
);
@@ -144,17 +147,6 @@ export function VisioJSSchematic({ setResults, setNodes, setComponentValues, set
144147
regenerateNodeMaps(history.state[history.pointer + 1]);
145148
}, [regenerateNodeMaps, setHistory, history, vjs]);
146149

147-
// useEffect(() => {
148-
// //in react safe-mode this is executed twice which really breaks d3 event listeners & drag behavior. Using a ref to prevent double-initialization
149-
// if (initializedRef.current) return;
150-
// initializedRef.current = true;
151-
// vjs = visiojs({
152-
// initialState: initialState,
153-
// stateChanged: trackHistory,
154-
// });
155-
// vjs.init();
156-
// });
157-
158150
useEffect(() => {
159151
if (!vjs) {
160152
var newVjs = visiojs({

src/visiojs_to_matrix.js

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function createNodeMap(newState, addShapes) {
3030

3131
var start, end, nodeFound, index, node;
3232
var vin_node = null;
33+
var crushedNodes = [];
3334
for (const w of newState.wires) {
3435
if (w == null) continue; //skip deleted wires
3536
const startEl = elementMapper(newState.shapes[w.start.shapeID]);
@@ -39,28 +40,88 @@ export function createNodeMap(newState, addShapes) {
3940
end = { ...endEl, port: w.end.connectorID };
4041
components[startEl.name].portConnections[w.start.connectorID] = true;
4142
components[endEl.name].portConnections[w.end.connectorID] = true;
43+
crushedNodes.push([start, end])
4244

43-
if (startEl.type == "gnd" || endEl.type == "gnd") continue; //don't add ground nodes to the node map
45+
// if (startEl.type == "gnd" || endEl.type == "gnd") continue; //don't add ground nodes to the node map
4446

45-
nodeFound = false;
46-
for (index in nodeMapPre) {
47-
node = nodeMapPre[index];
47+
// nodeFound = false;
48+
// for (index in nodeMapPre) {
49+
// node = nodeMapPre[index];
50+
// for (const conn_index in node) {
51+
// const conn = node[conn_index];
52+
// if (conn.name == start.name && conn.port == start.port) {
53+
// nodeMapPre[index].push(end);
54+
// nodeFound = true;
55+
// } else if (conn.name == end.name && conn.port == end.port) {
56+
// nodeMapPre[index].push(start);
57+
// nodeFound = true;
58+
// }
59+
// }
60+
// }
61+
// if (!nodeFound) nodeMapPre.push([start, end]);
62+
}
63+
// console.log("new node map", JSON.parse(JSON.stringify(nodeMapPre)));
64+
65+
var wasChanged
66+
do {
67+
wasChanged = false;
68+
outerLoop: for (index in crushedNodes) {
69+
// node = crushedNodes[index];
70+
for (const conn_index in crushedNodes[index]) {
71+
const conn = crushedNodes[index][conn_index];
72+
//check if conn exists in any other node, then merge the nodes
73+
for (const nodeIndex in crushedNodes) {
74+
if (nodeIndex == index) continue; //skip the current node
75+
const node = crushedNodes[nodeIndex];
76+
for (const conn2_index in node) {
77+
const conn2 = node[conn2_index];
78+
if (conn.name == conn2.name && conn.port == conn2.port) {
79+
// console.log("found a match", conn, conn2, "in node", node);
80+
//merge the nodes
81+
// if (nodeIndex != index) {
82+
// console.log("merging nodes", index, "and", nodeIndex);
83+
crushedNodes[nodeIndex] = [...crushedNodes[nodeIndex], ...crushedNodes[index]];
84+
crushedNodes.splice(index, 1);
85+
wasChanged = true;
86+
break outerLoop; //break to avoid checking the rest of the connections
87+
// }
88+
}
89+
}
90+
}
91+
}
92+
}
4893

49-
for (const conn_index in node) {
50-
const conn = node[conn_index];
51-
if (conn.name == start.name && conn.port == start.port) {
52-
nodeMapPre[index].push(end);
53-
nodeFound = true;
54-
} else if (conn.name == end.name && conn.port == end.port) {
55-
nodeMapPre[index].push(start);
56-
nodeFound = true;
94+
} while (wasChanged);
95+
96+
//delete the node containing gnd
97+
outerLoop : for (const index in crushedNodes) {
98+
const node = crushedNodes[index];
99+
for (const conn of node) {
100+
if (conn.type == "gnd") {
101+
// console.log("found gnd node", node, "at index", index);
102+
crushedNodes.splice(index, 1);
103+
break outerLoop; //break to avoid checking the rest of the connections
104+
}
105+
}
106+
}
107+
108+
//remove duplicates from crushedNodes
109+
for (const n in crushedNodes) {
110+
for (let i = 0; i < crushedNodes[n].length; i++) {
111+
const conn = crushedNodes[n][i];
112+
for (let j = i + 1; j < crushedNodes[n].length; j++) {
113+
const conn2 = crushedNodes[n][j];
114+
// console.log("conns", conn, conn2);
115+
if (conn.name == conn2.name && conn.port == conn2.port) {
116+
// console.log("found a duplicate", conn, "in node", crushedNodes[n]);
117+
crushedNodes[n].splice(j, 1); //remove the duplicate
118+
j--; //decrement j to account for the removed element
57119
}
58120
}
59121
}
60-
if (!nodeFound) nodeMapPre.push([start, end]);
61122
}
62-
// console.log("new node map", nodeMapPre);
63-
// console.log("components", components);
123+
// console.log("crushedNodes", crushedNodes);
124+
nodeMapPre = crushedNodes;
64125

65126
//remove components that are not fully connected
66127
for (const [key, value] of Object.entries(components)) {
@@ -105,6 +166,7 @@ export function createNodeMap(newState, addShapes) {
105166
// console.log("connected elements to vin", connected_elements);
106167

107168
// const nodeMap = nodeMapPre.filter(node => connected_nodes.includes(node));
169+
// console.log("pre map", nodeMapPre)
108170
nodeMap = connected_nodes.map((node) => nodeMapPre[node]);
109171
// console.log("final node map", nodeMap);
110172
//find all nodes that have a path to vin_node
@@ -121,16 +183,6 @@ export function createNodeMap(newState, addShapes) {
121183
};
122184
}
123185
fullyConnectedComponents[comp.name].ports[comp.port] = i;
124-
125-
// //FIXME - can these maps be eliminated now fullyConnectedComponents is more detailed?
126-
// if (comp.type == "opamp") {
127-
// if (!(comp.name in opAmpsMap)) opAmpsMap[comp.name] = [null, null, null];
128-
// opAmpsMap[comp.name][comp.port] = i;
129-
// // } else if (comp.type == "iprobe") {
130-
// // if (!(comp.name in iprbMap)) iprbMap[comp.name] = [null, null];
131-
// // iprbMap[comp.name][comp.port] = i;
132-
// // if (comp.port == 0) iprbNode = i;
133-
// }
134186
});
135187
});
136188
}

0 commit comments

Comments
 (0)