-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckt_solver.html
508 lines (454 loc) · 16.3 KB
/
ckt_solver.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<!DOCTYPE html>
<html>
<head>
<title>Circuit solver</title>
<!-- for online: -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://unpkg.com/mathjs/dist/math.min.js"></script>
<!-- for offline (if you have these packages downloaded, change the file path accordingly): -->
<!-- <script src="/home/adway/js_stuff/node_modules/jquery/dist/jquery.min.js"></script>
<script src="/home/adway/js_stuff/node_modules/mathjs/dist/math.min.js"></script> -->
<style>
#wrapper { /*to include tooltip with the canvas*/
position:relative;
width:300px;
height:150px;
}
canvas {
border:1px solid red;
}
#tip { /*for tooltip*/
background-color:white;
border:1px solid blue;
position:absolute;
left:-400px;
top:100px;
}
</style>
<body>
<br>Enter the Netlist:<br>
<textarea id="netlist" rows="15" cols="15"></textarea>
<button onclick="solver()">Solve</button>
<br><br>
<div id="wrapper">
<canvas id="myCanvas" width="1000" height="500"
style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<canvas id="tip" width=200 height=30></canvas>
</div>
<!---------------------------------------------------------------------------------------->
<!---------------------------------------------------------------------------------------->
<!----------------------------------- Start of script ------------------------------------>
<!---------------------------------------------------------------------------------------->
<script>
//-----------------------------------------------------------------------------------
// Read file
//-----------------------------------------------------------------------------------
function solver(){
var lines, n
var file = document.getElementById("netlist").value;
var no_of_nodes=0;
var resistors=[];
var curr_sources=[];
var volt_sources=[];
// read the netlist and display it on the window
var num = 0;
var N = 0;
lines = file.split('\n');
n=lines.length;
// find number of nodes and display the netlist in browser
for(var line = 0; line < n; line++){
words = lines[line].split(" ");
if((words[0])[0]=='r'){
n1=parseInt(words[1]);
n2=parseInt(words[2]);
num=Math.max(n1,n2);
N=Math.max(num,N);
r=parseFloat(words[3]);
resistors.push({res:r,n1:n1,n2:n2});
}
else if((words[0])[0]=='i'){
n1=parseInt(words[1]);
n2=parseInt(words[2]);
num=Math.max(n1,n2);
N=Math.max(num,N);
i=parseFloat(words[3]);
curr_sources.push({curr:i,n1:n1,n2:n2});
}
else if((words[0])[0]=='v'){
n1=parseInt(words[1]);
n2=parseInt(words[2]);
num=Math.max(n1,n2);
N=Math.max(num,N);
v=parseFloat(words[3]);
volt_sources.push({volt:v,n1:n1,n2:n2});
}
}
no_of_nodes = N+1;
//-----------------------------------------------------------------------------------
// Solve the circuit
//-----------------------------------------------------------------------------------
/*
using modified nodal analysis
A = [[G B] G is conductance matrix, B is incidence matrix of nodes and voltage sources
[C D]] C is transpose of B, D is just zeros (these change if dependent sources are present)
Z = [[i] i is sum of currents entering a node
[e]] e is value of voltage sources
X = [[v] v holds unknown voltages
[j]] j holds unknown current through voltage sources
AX=Z*/
var N=no_of_nodes;
var M=volt_sources.length;;
// for A
var G_temp=math.zeros(N,N)
var B_temp=math.zeros(N,M)
var D=math.zeros(M,M) // final D
// for X
var I_temp=math.zeros(N)
var E=math.zeros(M) // final E
for(var index=0; index<resistors.length; index++){ // resistors
var n1=resistors[index].n1;
var n2=resistors[index].n2;
var r = resistors[index].res;
G_temp.subset(math.index(n1,n1),math.subset(G_temp,math.index(n1,n1))+1/r);
G_temp.subset(math.index(n1,n2),math.subset(G_temp,math.index(n1,n2))-1/r);
G_temp.subset(math.index(n2,n1),math.subset(G_temp,math.index(n2,n1))-1/r);
G_temp.subset(math.index(n2,n2),math.subset(G_temp,math.index(n2,n2))+1/r);
}
var G = G_temp.subset(math.index(math.range(1,N),math.range(1,N))); // final G
var ind=0;
for(var index=0; index<volt_sources.length; index++){ // voltage sources
var n1=volt_sources[index].n1;
var n2=volt_sources[index].n2;
var v =volt_sources[index].volt;
B_temp.subset(math.index(n1,ind),1);
B_temp.subset(math.index(n2,ind),-1);
E.subset(math.index(ind),v);
ind+=1;
}
var B = B_temp.subset(math.index(math.range(1,N),math.range(0,M))); // final B
var C = math.transpose(B); // final C
var A1 = math.concat(G,B);
var A2 = math.concat(C,D);
var A = math.concat(A1,A2,0); // final A
for(var index=0; index<curr_sources.length; index++){ //current sources
var n1=curr_sources[index].n1;
var n2=curr_sources[index].n2;
var i =curr_sources[index].curr;
I_temp.subset(math.index(n1),math.subset(I_temp,math.index(n1))-i);
I_temp.subset(math.index(n2),math.subset(I_temp,math.index(n2))+i);
}
var I = I_temp.subset(math.index(math.range(1,N))); // final I
var Z = math.concat(I,E,0); // final Z
var A_inv = math.inv(A);
var X = math.multiply(A_inv,Z); // final X
var V = math.concat([0],X.subset(math.index(math.range(0,N-1))),0); // final voltages at nodes
// display
// for(var index=0; index<N; index++){
// document.getElementById("output").innerHTML += "Voltage at node "+index+" = "+math.subset(V,math.index(index))+"V\n";
// }
// for(var index=1; index<=M; index++){
// document.getElementById("output").innerHTML += "Current through voltage source "+index+" = "+math.subset(X,math.index(index+N-2))+"A\n";
// }
//-----------------------------------------------------------------------------------
// Display the circuit schematic
//-----------------------------------------------------------------------------------
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var tipCanvas = document.getElementById("tip"); // for tooltip to
var tipCtx = tipCanvas.getContext("2d"); // display voltage
tipCtx.font = "normal 15px Arial";
// ctx.clearRect(0, 0, canvas.width, canvas.height);
var connections = math.zeros(no_of_nodes,no_of_nodes);
var nodes_pos = set_nodes(ctx,no_of_nodes,V);
for(var index=0; index<resistors.length; index++){ // resistors
var n1=resistors[index].n1;
var n2=resistors[index].n2;
var r = resistors[index].res;
connections=disp_resistor(ctx,nodes_pos,n1,n2,r,connections,no_of_nodes);
}
for(var index=0; index<volt_sources.length; index++){ // voltage sources
var n1=volt_sources[index].n1;
var n2=volt_sources[index].n2;
var v =volt_sources[index].volt;
connections=disp_volt(ctx,nodes_pos,n1,n2,v,connections,no_of_nodes);
}
for(var index=0; index<curr_sources.length; index++){ //current sources
var n1=curr_sources[index].n1;
var n2=curr_sources[index].n2;
var i =curr_sources[index].curr;
connections=disp_curr(ctx,nodes_pos,n1,n2,i,connections,no_of_nodes);
}
//-----------------------------------------------------------------------------------
// Show voltage in tooltip when mouse is clicked
//-----------------------------------------------------------------------------------
var canvasOffset = $("#myCanvas").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
$("#myCanvas").mousemove(function (e) {
handleMouseMove(e);
});
// show tooltip when mouse hovers over dot
function handleMouseMove(e) {
// show tooltip when mouse hovers over dot
mouseX = e.pageX - offsetX;
mouseY = e.pageY - offsetY;
// Put your mousemove stuff here
var hit = false;
node = detect_node(mouseX,mouseY,nodes_pos,no_of_nodes);
if(node>-1){
//document.write(node);
tipCanvas.style.left = (nodes_pos[node].x)+"px";
tipCanvas.style.top = (nodes_pos[node].y - 40)+"px";
tipCtx.clearRect(0, 0, tipCanvas.width, tipCanvas.height);
tipCtx.fillText(nodes_pos[node].tip, 5, 20);
//tipCtx.fillText(nodes_pos[node].x.toString(),5,15);
hit = true;
}
if (!hit) {
tipCanvas.style.left = "-500px"; }
}
//-----------------------------------------------------------------------------------
// End of main code
//-----------------------------------------------------------------------------------
};
//-----------------------------------------------------------------------------------
//--------------------- Some functions that are used in the code --------------------
//-----------------------------------------------------------------------------------
function disp_resistor(ctx,nodes_pos,n1,n2,r,connections,N){ // display resistors
var n1_x=nodes_pos[n1].x;
var n1_y=nodes_pos[n1].y;
var n2_x=nodes_pos[n2].x;
var n2_y=nodes_pos[n2].y;
var len=50;
var mid_x=(n1_x+n2_x)/2;
var mid_y=(n1_y+n2_y)/2;
var angle = Math.atan2(n2_y-n1_y,n2_x-n1_x);
var x_new = n1_x;
var y_new = n1_y;
ctx.moveTo(x_new,y_new);
var shift=elt(connections,n1,n2)+elt(connections,n2,n1);
var sep = 80*Math.abs(n1-n2);
if((n1== 0 || n2 == 0)&&(n1>N/2 || n2> N/2)){
sep = 80*N-sep;
}
// to deal with parallel components
if((n1!=0 && n2!=0 && n2>n1) || (n2==0 && n1>N/2) || (n1==0 && n2<=N/2)){
for(var i = 0; i<shift;i++){
x_new -= sep*Math.sin(angle);
y_new += sep*Math.cos(angle);
}
}
else{ //if((n1!=0 && n2!=0 && n1>n2) || (n1==0 && n2>N/2) || (n2==0 && n1<=N/2)){
for(var i = 0; i<shift;i++){
x_new += sep*Math.sin(angle);
y_new -= sep*Math.cos(angle);
}
}
// draw the resistor
ctx.lineTo(x_new,y_new);
x_new=mid_x-n1_x-len/2*Math.cos(angle)+x_new;
y_new=mid_y-n1_y-len/2*Math.sin(angle)+y_new;
ctx.lineTo(x_new,y_new);
for(var i = 0; i<4;i++){
x_new+=len/8*Math.cos(angle-Math.PI/3);
y_new+=len/8*Math.sin(angle-Math.PI/3);
ctx.lineTo(x_new,y_new);
x_new+=len/4*Math.cos(Math.PI/3+angle);
y_new+=len/4*Math.sin(Math.PI/3+angle);
ctx.lineTo(x_new,y_new);
x_new+=len/8*Math.cos(angle-Math.PI/3);
y_new+=len/8*Math.sin(angle-Math.PI/3);
ctx.lineTo(x_new,y_new);
}
// again to deal with parallel components
x_new = n2_x;
y_new = n2_y;
if((n1!=0 && n2!=0 && n2>n1) || (n2==0 && n1>N/2) || (n1==0 && n2<=N/2)){
for(var i = 0; i<shift;i++){
x_new -= sep*Math.sin(angle);
y_new += sep*Math.cos(angle);
}
}
else if((n1!=0 && n2!=0 && n1>n2) || (n1==0 && n2>N/2) || (n2==0 && n1<=N/2)){
for(var i = 0; i<shift;i++){
x_new += sep*Math.sin(angle);
y_new -= sep*Math.cos(angle);
}
}
ctx.lineTo(x_new,y_new);
ctx.lineTo(n2_x,n2_y);
ctx.stroke();
connections.subset(math.index(n1,n2),math.subset(connections,math.index(n1,n2))+1);
return connections;
}
function disp_volt(ctx,nodes_pos,n1,n2,v,connections,N){ // display voltage sources
var n1_x=nodes_pos[n1].x;
var n1_y=nodes_pos[n1].y;
var n2_x=nodes_pos[n2].x;
var n2_y=nodes_pos[n2].y;
var len=40;
var angle = Math.atan2(n2_y-n1_y,n2_x-n1_x);
ctx.moveTo(n1_x,n1_y);
var n1_x_new = n1_x;
var n1_y_new = n1_y;
var n2_x_new = n2_x;
var n2_y_new = n2_y;
var shift=elt(connections,n1,n2)+elt(connections,n2,n1);
var sep = 80*Math.abs(n1-n2);
if((n1== 0 || n2 == 0)&&(n1>N/2 || n2> N/2)){
sep = 80*N-sep;
}
if((n1!=0 && n2!=0 && n2>n1) || (n2==0 && n1>N/2) || (n1==0 && n2<=N/2)){
for(var i = 0; i<shift;i++){
n1_x_new -= sep*Math.sin(angle);
n1_y_new += sep*Math.cos(angle);
n2_x_new -= sep*Math.sin(angle);
n2_y_new += sep*Math.cos(angle);
}
}
else if((n1!=0 && n2!=0 && n1>n2) || (n1==0 && n2>N/2) || (n2==0 && n1<=N/2)){
for(var i = 0; i<shift;i++){
n1_x_new += sep*Math.sin(angle);
n1_y_new -= sep*Math.cos(angle);
n2_x_new += sep*Math.sin(angle);
n2_y_new -= sep*Math.cos(angle);
}
}
ctx.lineTo(n1_x_new,n1_y_new);
var mid_x=(n1_x_new+n2_x_new)/2;
var mid_y=(n1_y_new+n2_y_new)/2;
ctx.lineTo(mid_x-len/2*Math.cos(angle),mid_y-len/2*Math.sin(angle));
// draw circle
ctx.moveTo(mid_x+len/2,mid_y);
ctx.arc(mid_x, mid_y, len/2, 0, 2 * Math.PI);
//put +
ctx.moveTo(mid_x-3*len/8*Math.cos(angle),mid_y-3*len/8*Math.sin(angle));
ctx.lineTo(mid_x-len/8*Math.cos(angle),mid_y-len/8*Math.sin(angle));
var x_new=mid_x-len/4*Math.cos(angle);
var y_new=mid_y-len/4*Math.sin(angle);
x_new = x_new-len/8*Math.sin(angle);
y_new = y_new+len/8*Math.cos(angle);
ctx.moveTo(x_new,y_new);
ctx.lineTo(x_new+len/4*Math.sin(angle),y_new-len/4*Math.cos(angle));
// put -
var x_new=mid_x+len/4*Math.cos(angle);
var y_new=mid_y+len/4*Math.sin(angle);
x_new = x_new+len/8*Math.sin(angle);
y_new = y_new-len/8*Math.cos(angle);
ctx.moveTo(x_new,y_new);
ctx.lineTo(x_new-len/4*Math.sin(angle),y_new+len/4*Math.cos(angle));
ctx.moveTo(mid_x+len/2*Math.cos(angle),mid_y+len/2*Math.sin(angle));
ctx.lineTo(n2_x_new,n2_y_new);
ctx.lineTo(n2_x,n2_y);
ctx.stroke();
connections.subset(math.index(n1,n2),math.subset(connections,math.index(n1,n2))+1);
return connections;
}
function disp_curr(ctx,nodes_pos,n1,n2,i,connections,N){ // display current sources
var n1_x=nodes_pos[n1].x;
var n1_y=nodes_pos[n1].y;
var n2_x=nodes_pos[n2].x;
var n2_y=nodes_pos[n2].y;
var len=40;
var angle = Math.atan2(n2_y-n1_y,n2_x-n1_x);
ctx.moveTo(n1_x,n1_y);
var n1_x_new = n1_x;
var n1_y_new = n1_y;
var n2_x_new = n2_x;
var n2_y_new = n2_y;
var shift=elt(connections,n1,n2)+elt(connections,n2,n1);
var sep = 80*Math.abs(n1-n2);
if((n1== 0 || n2 == 0)&&(n1>N/2 || n2> N/2)){
sep = 80*N-sep;
}
if((n1!=0 && n2!=0 && n2>n1) || (n2==0 && n1>N/2) || (n1==0 && n2<=N/2)){
for(var i = 0; i<shift;i++){
n1_x_new -= sep*Math.sin(angle);
n1_y_new += sep*Math.cos(angle);
n2_x_new -= sep*Math.sin(angle);
n2_y_new += sep*Math.cos(angle);
}
}
else if((n1!=0 && n2!=0 && n1>n2) || (n1==0 && n2>N/2) || (n2==0 && n1<=N/2)){
for(var i = 0; i<shift;i++){
n1_x_new += sep*Math.sin(angle);
n1_y_new -= sep*Math.cos(angle);
n2_x_new += sep*Math.sin(angle);
n2_y_new -= sep*Math.cos(angle);
}
}
ctx.lineTo(n1_x_new,n1_y_new);
var mid_x=(n1_x_new+n2_x_new)/2;
var mid_y=(n1_y_new+n2_y_new)/2;
ctx.lineTo(mid_x-len/2*Math.cos(angle),mid_y-len/2*Math.sin(angle));
// draw circle
ctx.moveTo(mid_x+len/2,mid_y);
ctx.arc(mid_x, mid_y, len/2, 0, 2 * Math.PI);
// put line
ctx.moveTo(mid_x-3*len/8*Math.cos(angle),mid_y-3*len/8*Math.sin(angle));
ctx.lineTo(mid_x+3*len/8*Math.cos(angle),mid_y+3*len/8*Math.sin(angle));
// put arrowhead
var x_new=mid_x+len/4*Math.cos(angle);
var y_new=mid_y+len/4*Math.sin(angle);
x_new = x_new+len/8*Math.sin(angle);
y_new = y_new-len/8*Math.cos(angle);
ctx.lineTo(x_new,y_new);
x_new=mid_x+len/4*Math.cos(angle);
y_new=mid_y+len/4*Math.sin(angle);
x_new = x_new-len/8*Math.sin(angle);
y_new = y_new+len/8*Math.cos(angle);
ctx.moveTo(x_new,y_new);
ctx.lineTo(mid_x+3*len/8*Math.cos(angle),mid_y+3*len/8*Math.sin(angle));
ctx.moveTo(mid_x+len/2*Math.cos(angle),mid_y+len/2*Math.sin(angle));
ctx.lineTo(n2_x_new,n2_y_new);
ctx.lineTo(n2_x,n2_y);
ctx.stroke();
connections.subset(math.index(n1,n2),math.subset(connections,math.index(n1,n2))+1);
return connections;
}
function set_nodes(ctx,n,V){ // set the nodes on a blank screen
var nodes=[];
var theta=2*Math.PI/n;
var curr_theta=theta;
var x_0=500;
var y_0=250;
var R=120;
for(var index=0; index<n; index++){
var x=x_0+R*Math.sin(curr_theta);
var y=y_0+R*Math.cos(curr_theta);
nodes.push({
x:x,
y:y,
r:6,
tip: "Node: "+index.toString()+", Voltage: "+math.subset(V,math.index(index)).toFixed(2).toString()+" V\n"
});
curr_theta=curr_theta+theta;
ctx.moveTo(x+6,y); // radius of node
ctx.arc(x,y,6,0,2*Math.PI); // is 6
ctx.fillStyle='red';
ctx.fill();
}
return nodes;
}
function detect_node(x,y,nodes_pos){ // identify the node which is clicked
for(var index=0; index<nodes_pos.length; index++){
var x1=nodes_pos[index].x;
var y1=nodes_pos[index].y;
if(Math.sqrt(Math.pow(x-x1,2)+Math.pow(y-y1,2)) < 9){ // radius of node is 6, relax it a little
return index;
}
}
return -1;
}
function elt(A, n1, n2){ // return element at position (n1,n2) in matrix A
return math.subset(A,math.index(n1,n2));
}
</script>
<!---------------------------------------------------------------------------------------->
<!---------------------------------------------------------------------------------------->
<!----------------------------------- End of script -------------------------------------->
<!---------------------------------------------------------------------------------------->
</body>
</html>