Skip to content

Commit 743bafe

Browse files
committed
prevent op amp causing crash. Fix op amp with current input
1 parent 1a3dfe1 commit 743bafe

3 files changed

Lines changed: 48 additions & 46 deletions

File tree

circuitSolver/modules/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,16 @@ class Game extends React.Component {
780780
var fmax = current.fmax.value * unitStrToVal(current.fmax.unit);
781781
var fstepdB_20 = Math.log10(fmax / fmin) / current.numSteps;
782782
var fstep = 10 ** fstepdB_20;
783+
var absNew, evalNew;
783784
// console.log(fmin, fmax, fstep)
784785
// console.log(fmin, fmax, fstep, fstepdB_20, this.freq)
785786
// console.log('bp9', this.resString, res)
786787

787788
for (var f = fmin; f < fmax; f = f * fstep) {
788789
this.freq.push(f);
789-
this.mag.push(20 * Math.log10(Math.abs(eval(res.replace(re, 2 * Math.PI * f)))));
790+
evalNew = eval(res.replace(re, 2 * Math.PI * f))
791+
absNew = Math.abs(evalNew);
792+
this.mag.push(20 * Math.log10(absNew));
790793
}
791794

792795
// console.log("response: ", this.freq, this.mag )

circuitSolver/modules/mna.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,18 @@ export function calculateMNA(canvasState, chosenPlot) {
168168
if (debug) console.log("allElements", allElements)
169169
if (debug) console.log("iinOrVin", iinOrVin)
170170

171+
schematicReadiness.solvable = false;
172+
171173
// Build MNA array
172174
if (schematicReadiness.vout && schematicReadiness.vin && schematicReadiness.gnd) {
175+
var extraRow = 0;
176+
if (iinOrVin == "vin") extraRow = 1;
173177
var numOpAmps = 0;
174178
for (const key in usedElements) if (Array.from(key)[0] == "o") numOpAmps += 1;
175179
var numIprb = 0;
176180
for (const key in usedElements) if (Array.from(key)[0] == "Y") numIprb += 1; //FIXME - confirm this is used
177181
// Create 2D modified nodal analysis array
178-
if (iinOrVin == "vin") var mnaMatrix = new Array(nodeMap.length + 1 + numOpAmps + numIprb);
179-
else var mnaMatrix = new Array(nodeMap.length + numOpAmps + numIprb);
182+
var mnaMatrix = new Array(nodeMap.length + extraRow + numOpAmps + numIprb);
180183
for (i = 0; i < mnaMatrix.length; i++) mnaMatrix[i] = new Array(mnaMatrix.length).fill("0");
181184

182185
// Step 1 - create map of every element and which node it connects too. Doing this here, after node map is complete and ground node is removed
@@ -245,50 +248,50 @@ export function calculateMNA(canvasState, chosenPlot) {
245248
mnaMatrix[vinNode][mnaMatrix.length - 1 - numOpAmps - numIprb] = "1";
246249
}
247250

248-
//2.5 For each op-amp add some 1's. It says that 2 nodes are equal to each other, and that 1 node has a new ideal current source
249-
// var opAmp = 0;
250-
//port 0 -> +
251-
//port 1 -> -
252-
//port 2 -> out
253-
// console.log('pre-op', mnaMatrix);
254-
var idx;
255-
for (const key in opAmpsMap) {
256-
idx = parseInt(key);
257-
if (opAmpsMap[key][0] != null) mnaMatrix[nodeMap.length + 1 + idx][opAmpsMap[idx][0]] = "1";
258-
if (opAmpsMap[key][1] != null) mnaMatrix[nodeMap.length + 1 + idx][opAmpsMap[idx][1]] = "-1";
259-
if (opAmpsMap[key][2] != null) mnaMatrix[opAmpsMap[idx][2]][nodeMap.length + 1 + idx] = "1";
260-
}
261-
262-
//2.6 Current probes. The last rows are for current probes. 4x 1's are inserted to the Matrix, unless one node is ground
263-
var iprbCounter=0;
264-
for (const key in iprbMap) {
265-
idx = parseInt(key);
266-
if (iprbMap[key][0] != null) {
267-
mnaMatrix[nodeMap.length + 1 + numOpAmps + iprbCounter][iprbMap[idx][0]] = "1";
268-
mnaMatrix[iprbMap[idx][0]][ nodeMap.length + 1 + numOpAmps + iprbCounter] = "1";
251+
try {
252+
var idx;
253+
//2.5 For each op-amp add some 1's. It says that 2 nodes are equal to each other, and that 1 node has a new ideal current source
254+
// var opAmp = 0;
255+
//port 0 -> +
256+
//port 1 -> -
257+
//port 2 -> out
258+
// console.log('pre-op', mnaMatrix, nodeMap.length, opAmpsMap);
259+
for (const key in opAmpsMap) {
260+
idx = parseInt(key);
261+
if (opAmpsMap[key][0] != null) mnaMatrix[nodeMap.length + extraRow + idx][opAmpsMap[idx][0]] = "1";
262+
if (opAmpsMap[key][1] != null) mnaMatrix[nodeMap.length + extraRow + idx][opAmpsMap[idx][1]] = "-1";
263+
if (opAmpsMap[key][2] != null) mnaMatrix[opAmpsMap[idx][2]][nodeMap.length + extraRow + idx] = "1";
269264
}
270-
if (iprbMap[key][1] != null) {
271-
mnaMatrix[nodeMap.length + 1 + numOpAmps + iprbCounter][iprbMap[idx][1]] = "-1";
272-
mnaMatrix[iprbMap[idx][1]][nodeMap.length + 1 + numOpAmps + iprbCounter] = "-1";
265+
266+
//2.6 Current probes. The last rows are for current probes. 4x 1's are inserted to the Matrix, unless one node is ground
267+
var iprbCounter=0;
268+
for (const key in iprbMap) {
269+
idx = parseInt(key);
270+
if (iprbMap[key][0] != null) {
271+
mnaMatrix[nodeMap.length + 1 + numOpAmps + iprbCounter][iprbMap[idx][0]] = "1";
272+
mnaMatrix[iprbMap[idx][0]][ nodeMap.length + 1 + numOpAmps + iprbCounter] = "1";
273+
}
274+
if (iprbMap[key][1] != null) {
275+
mnaMatrix[nodeMap.length + 1 + numOpAmps + iprbCounter][iprbMap[idx][1]] = "-1";
276+
mnaMatrix[iprbMap[idx][1]][nodeMap.length + 1 + numOpAmps + iprbCounter] = "-1";
277+
}
278+
iprbCounter = iprbCounter+1;
273279
}
274-
iprbCounter = iprbCounter+1;
275-
}
276280

277-
var nerdStrArr = [];
278-
var nerdStr = "";
279-
for (i = 0; i < mnaMatrix.length; i++) {
280-
nerdStrArr.push("[" + mnaMatrix[i].join(",") + "]");
281-
}
282-
nerdStr = nerdStrArr.join(",");
283-
if (debug) console.log('mnaMatrix', mnaMatrix);
281+
var nerdStrArr = [];
282+
var nerdStr = "";
283+
for (i = 0; i < mnaMatrix.length; i++) {
284+
nerdStrArr.push("[" + mnaMatrix[i].join(",") + "]");
285+
}
286+
nerdStr = nerdStrArr.join(",");
287+
if (debug) console.log('mnaMatrix', mnaMatrix);
284288

285-
//Using algebrite not nerdamer
286-
// const start = Date.now();
287-
Algebrite.eval("clearall");
288-
Algebrite.eval("mna = [" + nerdStr + "]");
289-
var resString, resMathML;
289+
//Using algebrite not nerdamer
290+
// const start = Date.now();
291+
Algebrite.eval("clearall");
292+
Algebrite.eval("mna = [" + nerdStr + "]");
293+
var resString, resMathML;
290294

291-
try {
292295
if (mnaMatrix.length == 1) {
293296
Algebrite.eval(`mna_vo_vi = 1/(${mnaMatrix[0]})`);
294297
} else {
@@ -326,7 +329,6 @@ export function calculateMNA(canvasState, chosenPlot) {
326329
schematicReadiness.solvable = true;
327330
} catch (err) {
328331
console.log("Solving failed with this error:", err);
329-
schematicReadiness.solvable = false;
330332
resMathML = "<mtext>Schematic currently invalid</mtext>";
331333
resString = "";
332334
}
@@ -336,7 +338,6 @@ export function calculateMNA(canvasState, chosenPlot) {
336338
} else {
337339
resMathML = "<mtext>Schematic currently invalid</mtext>";
338340
// bilinearMathML = "<mtext>Schematic currently invalid</mtext>";
339-
schematicReadiness.solvable = false;
340341
}
341342

342343
// console.log('bp2', resString)

circuitSolver/toDo.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
- if add op amp with zero on input and LC on output, and change input to current source, it crashes
2-
-- http://127.0.0.1:8000/?state=eJy1lM1ygjAUhd8laxYkRbRsWXQh0wVddrqgkCozkDAQHBnHd%252B8NYBW4ZqC1G1Hv%252BXLuHzkRnvGcC1UR70RCW38eoqzmxCPUtolFapEq%252BLGFr0laFVnUvEa5DoP4bBGf3iD0R15M5CAEeTiQ26bjOz0bZGTMh2kgwPOpJ%252FKgPd9nc9OH00H%252FlacCJbakjUbHQX3XAl90XNT5m%252BJF1YeqeM%252FzSKUx8d5PRDWFNoplXkgBE4EU0gT%252BOICjRSKxyyAMFDhQF54N8djKPVv3ydAeg4w5LUjXRnAnkjH5xDpLeJrI40HWasxu7I6lz8zE%252BvQKsnWfsNOj1IiGCHrJGGo2obKYdMlxNp2pa6wVFu4OyNbGbAM6BlfuvA5NHN1%252BoNCnhQO9kOOBCsFjlUq9c5Wsy5jrhRYy4TfTLWSpd3rffJZp0l4DKip3XN1I21THutlGIUYjLt3wBkK6xIYiNGLTte%252FX1Ux78S82SDPwrk3LXjQbjMZc%252FjQaf%252BZoHu0yt5ZFHZs5l%252B6mf%252BBLg7cM2RO2wCaY2bM%252F2qAXCGIzTUcvwMf5G0K1u7Q%253D
3-
42

53

64

0 commit comments

Comments
 (0)