@@ -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)
0 commit comments