-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcircuit.cpp
2412 lines (2262 loc) · 61.2 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include"circuit.h"
int meshcnt=0;
CIRCUIT c;
STACK_ELEMENT *STACK;
int STACK_LENGTH=-1;
double *sa;
unsigned long *ija;
variable node_name(opts o,interpreter *i)
{
variable v=init_variable();
int N=0;
char *name=NULL;
if (i->cast_int(&N,o,0) && i->cast_string(&name,o,1))
{
node_name(N,c.BLOCK,name);
}
return v;
}
void node_name(int NODE,char *block,char *name)
{
// cout << "Setting node name for node " << NODE << "\n";
for(int x=0;x<c.n_nodes;x++)
{
if (c.nodes[x].N==NODE && strcmp(c.nodes[x].BLOCK,block)==0)
{
// cout << "Found node!\n";
c.nodes[x].NAME=(char *)malloc(sizeof(char)*(strlen(name)+1));
strcpy(c.nodes[x].NAME,name);
}
}
}
//Searches the node tree for a particular node number. Returns -1 if node number can't be found.
int check_for_node(int N,char *block)
{
for(int x=0;x<c.n_nodes;x++)
{
if (c.nodes[x].N==N && strcmp(c.nodes[x].BLOCK,block)==0)
return x;
}
return -1;
}
//Searches the mesh tree for a particular mesh number. Returns -1 if mesh number can't be found.
int check_for_mesh(int N)
{
for(int x=0;x<c.n_meshes;x++)
{
if (c.meshes[x].N==N)
return x;
}
return -1;
}
variable CIRC_add_monitor(opts o,interpreter *i)
{
variable ret=init_variable();
int type;
int NODE_1=-1;
int NODE_2=-1;
char *element;
char *BLOCK;
int buffer=-1;
type=0;
element=0;
if (i->cast_int(&type,o,0) && i->cast_string(&element,o,1))
{
if (!i->cast_string(&BLOCK,o,2))
BLOCK=c.BLOCK;
if (find_element(element,BLOCK)!=-1)
buffer=CIRC_add_monitor(type,find_element(element,BLOCK));
else if (find_element(element)!=-1)
buffer=CIRC_add_monitor(type,find_element(element));
}
else if (i->cast_int(&type,o,0) && i->cast_int(&NODE_1,o,1) && i->cast_int(&NODE_2,o,2))
{
buffer=CIRC_add_monitor(type,NODE_1,NODE_2);
}
ret=i->int_variable(buffer);
return ret;
}
int find_element(char *element,char *block)
{
for(int x=1;x<=c.n_elements;x++)
if (!strcmp(c.elements[x].name,element) && !strcmp(block,c.elements[x].BLOCK))
return x;
return -1;
}
int find_element(char *element)
{
for(int x=1;x<=c.n_elements;x++)
if (!strcmp(c.elements[x].name,element))
return x;
return -1;
}
int CIRC_get_node(int N,char *block)
{
int x;
for(x=0;x<c.n_nodes;x++)
{
if (c.nodes[x].N==N && strcmp(block,c.nodes[x].BLOCK)==0)
return x;
}
return -1;
}
//Returns the internal node number of a design node number.
int CIRC_get_node(char *name,char *block)
{
int x;
for(x=0;x<c.n_nodes;x++)
{
if (strcmp(c.nodes[x].NAME,name)==0 && strcmp(block,c.nodes[x].BLOCK)==0)
return x;
}
return -1;
}
//This is a monitor between two nodes (only current and phase!).
int CIRC_add_monitor(int type,int NODE_1,int NODE_2)
{
char buffer[10000];
char filename[1024];
const char *legend=NULL;
if (type==MONITOR_CURRENT)
return -1;
c.MONITORS=(MONITOR *)realloc((void *)c.MONITORS,sizeof(MONITOR)*(c.n_monitors+++1));
c.MONITORS[c.n_monitors-1].type=type;
c.MONITORS[c.n_monitors-1].element=-1;
c.MONITORS[c.n_monitors-1].NODE_1=CIRC_get_node(NODE_1,c.BLOCK);//replace this!!!
c.MONITORS[c.n_monitors-1].NODE_2=CIRC_get_node(NODE_2,c.BLOCK);//replace this!!!
switch (type)
{
case MONITOR_VOLTAGE:legend="V";break;
case MONITOR_PHASE:legend="Phi";break;
default:legend="undef.";break;
}
sprintf(buffer,"%s(%d->%d)",legend,NODE_1,NODE_2);
c.MONITORS[c.n_monitors-1].buffer=new_buffer(buffer,buffer);
return c.MONITORS[c.n_monitors-1].buffer;
}
//This is a voltage monitor.
int CIRC_add_monitor(int type,int element)
{
char buffer[10000];
const char *legend=NULL;
c.MONITORS=(MONITOR *)realloc((void *)c.MONITORS,sizeof(MONITOR)*(c.n_monitors+++1));
c.MONITORS[c.n_monitors-1].type=type;
c.MONITORS[c.n_monitors-1].element=element;
switch (type)
{
case MONITOR_VOLTAGE:legend="V";break;
case MONITOR_CURRENT:legend="I";break;
case MONITOR_PHASE:legend="Phi";break;
default:legend="undef.";break;
}
sprintf(buffer,"%s(%s)",legend,c.elements[element].name);
c.MONITORS[c.n_monitors-1].buffer=new_buffer(buffer);
return c.MONITORS[c.n_monitors-1].buffer;
}
variable build_trees(opts o,interpreter *i)
{
variable ret=init_variable();
build_trees();
return ret;
}
variable CIRC_solve(opts o,interpreter *i)
{
variable ret=init_variable();
CIRC_solve();
return ret;
}
variable CIRC_evolve(opts o,interpreter *i)
{
variable ret=init_variable();
CIRC_evolve();
return ret;
}
void CIRC_delete_node(int N)
{
int x;
int copy=0;
for(x=0;x<c.n_nodes-1;x++)
{
if (c.nodes[x].N==N)
{
if (c.nodes[x].NAME!=NULL)
free(c.nodes[x].NAME);
copy=1;
}
if (copy)
c.nodes[x]=c.nodes[x+1];
}
if (copy)
c.n_nodes--;
}
void CIRC_delete_element(int N)
{
int x;
int copy=0;
for(x=N;x<c.n_elements;x++)
{
c.elements[x]=c.elements[x+1];
}
c.n_elements--;
}
void clean_from_node(int a,int e)
{
int x;
int copy=0;
for(x=0;x<c.nodes[a].n_elements-1;x++)
{
if (c.nodes[a].elements[x]==e)
copy=1;
if (copy)
c.nodes[a].elements[x]=c.nodes[a].elements[x+1];
}
if (copy)
c.nodes[a].n_elements--;
}
//Does exactly what it says.
void eliminate_connections(void)
{
int x,y,z;
int a,b;
for(x=1;x<=c.n_elements;x++)
{
if (c.elements[x].TYPE==TYPE_CON)
{
a=c.elements[x].NODE_1;
b=c.elements[x].NODE_2;
clean_from_node(a,x);
clean_from_node(b,x);
// cout << "Eliminating connection from " << a << " to " << b << "\n";
for(y=1;y<=c.n_elements;y++)
{
if (y!=x)
{
if (c.elements[y].NODE_1==a)
{
c.elements[y].NODE_1=b;
}
if (c.elements[y].NODE_2==a)
{
c.elements[y].NODE_2=b;
}
}
}
// cout << "Eliminating " << c.elements[x].name << "\n";
CIRC_delete_element(x);
CIRC_delete_node(a);
x--;
}
// c.elements[x].BLOCK="MAIN";
}
}
void build_trees()
{
char str[1024];
int x,y,z,n_count;
double d=0.0;
int *indx;
double **temp;
int solver=c.SOLVER;
eliminate_connections();
build_flat_node_tree();
// print_node_tree();
build_mesh_tree();
int n1=0;
int n=0;
// gauss_elimination(c.mesh_matrix,c.n_elements);
// gauss_elimination(c.node_matrix,c.n_elements);
for(x=1;x<=c.n_elements;x++)
{
for(y=1;y<=c.n_elements;y++)
{
if (c.mesh_matrix[x][y]!=0)
n1++;
}
}
for(x=1;x<=c.n_elements;x++)
{
for(y=1;y<=c.n_elements;y++)
{
if (c.node_matrix[x][y]!=0)
n++;
}
}
cout << "saved nodes:" << (c.n_node_elements-n) << "\n";
cout << "saved meshs:" << (c.n_mesh_elements-n1) << "\n";
// print_mesh_tree();
c.I=(double *)realloc((void *)c.I,sizeof(double)*(c.n_elements+1));
c.V=(double *)realloc((void *)c.V,sizeof(double)*(c.n_elements+1));
c.phi=(double *)realloc((void *)c.phi,sizeof(double)*(c.n_elements+1));
c.I_int=(double *)realloc((void *)c.I_int,sizeof(double)*(c.n_elements+1));
c.V_int=(double *)realloc((void *)c.V_int,sizeof(double)*(c.n_elements+1));
c.I_p=(double *)realloc((void *)c.I_p,sizeof(double)*(c.n_elements+1));
c.V_p=(double *)realloc((void *)c.V_p,sizeof(double)*(c.n_elements+1));
c.phi_p=(double *)realloc((void *)c.phi_p,sizeof(double)*(c.n_elements+1));
c.I_pp=(double *)realloc((void *)c.I_pp,sizeof(double)*(c.n_elements+1));
c.V_pp=(double *)realloc((void *)c.V_pp,sizeof(double)*(c.n_elements+1));
c.phi_pp=(double *)realloc((void *)c.phi_pp,sizeof(double)*(c.n_elements+1));
//free_matrix(c.fjac,1,2*c.n_elements,1,2*c.n_elements);
c.fjac=matrix(1,2*c.n_elements,1,2*c.n_elements);
c.voltage_matrix=matrix(1,c.n_elements,1,c.n_elements);
c.current_matrix=matrix(1,c.n_elements,1,c.n_elements);
c.inv_voltage_matrix=matrix(1,c.n_elements,1,c.n_elements);
c.inv_current_matrix=matrix(1,c.n_elements,1,c.n_elements);
c.lin=matrix(1,2*c.n_elements,1,2*c.n_elements);
temp=matrix(1,2*c.n_elements,1,2*c.n_elements);
c.inverse=matrix(1,2*c.n_elements,1,2*c.n_elements);
c.map=imatrix(1,2*c.n_elements,1,2*c.n_elements);
c.lin_save=matrix(1,2*c.n_elements,1,2*c.n_elements);
c.vec=vector(1,2*c.n_elements);
c.solution=vector(1,2*c.n_elements);
c.vec_save=vector(1,2*c.n_elements);
c.indx=ivector(1,2*c.n_elements);
c.sparse_n=generate_linear_matrix(c.lin,c.vec,0.0)*2;
double sum=0.0;
for(x=1;x<=2*c.n_elements;x++)
for(y=1;y<=2*c.n_elements;y++)
{
c.map[x][y]=0;
c.inverse[x][y]=x==y?1.0:0.0;
}
c.sparse_vec=vector(1,c.sparse_n);
c.sparse_ind=lvector(1,c.sparse_n);
sa=c.sparse_vec;
ija=c.sparse_ind;
sprsin(c.lin,c.map,2*c.n_elements,THLIN,c.sparse_n,sa,ija);
for(x=1;x<=2*c.n_elements;x++)
memcpy(temp[x],c.lin[x],sizeof(double)*(c.n_elements*2+1));
sprsin(c.lin,c.map,2*c.n_elements,THLIN,c.sparse_n,sa,ija);
// linbcg(2*c.n_elements,c.vec,c.solution,2,1e-6,0,&iter,&err,c.sparse_vec,c.sparse_ind,c.inverse_vec,c.inverse_ind);
indx=ivector(1,2*c.n_elements);
clock_t t1=clock();
// full_gauss_elimination(c.lin,c.inverse,2*c.n_elements);
/*
ludcmp(c.lin,2*c.n_elements,indx,&d);
double *col=vector(1,2*c.n_elements);
int i,j;
n_count=0;
cout << "Done with LU...\n";
for(j=1;j<=2*c.n_elements;j++)
{
for(i=1;i<=2*c.n_elements;i++)
col[i]=0.0;
col[j]=1.0;
cout << j << "\n";
lubksb(c.lin,2*c.n_elements,indx,col);
for(i=1;i<=2*c.n_elements;i++)
{
c.inverse[i][j]=col[i];
if (fabs(c.inverse[i][j])>=TH/10.0)
n_count++;
}
}*/
gaussj(c.lin,2*c.n_elements,c.inverse,2*c.n_elements);
clock_t t2=clock();
sprintf(str,"%.4lf seconds for inverting A\n", (t2-t1)/(double)CLOCKS_PER_SEC);
cout << str;
n_count=4.0*c.n_elements*c.n_elements;
// full_gauss_elimination(c.lin,2*c.n_elements);
c.inverse_vec=vector(1,n_count+1);
c.inverse_ind=lvector(1,n_count+1);
cout << "N:" << n_count << "\n";
sprsin(c.inverse,NULL,2*c.n_elements,TH,n_count,c.inverse_vec,c.inverse_ind);
//free_matrix(c.lin,1,2*c.n_elements,1,2*c.n_elements);
for(x=1;x<=2*c.n_elements;x++)
c.solution[x]=0.0;
memcpy(&(c.vec_save[1]),&(c.vec[1]),(2*(c.n_elements))*sizeof(double));
// init_jacobian(c.fjac);
for(x=1;x<=2*c.n_elements;x++)
memcpy(c.lin[x],temp[x],sizeof(double)*(c.n_elements*2+1));
for(x=1;x<=2*c.n_elements;x++)
memcpy(&(c.lin_save[x][1]),&(c.lin[x][1]),(2*(c.n_elements))*sizeof(double));
free_matrix(temp,1,2*c.n_elements,1,2*c.n_elements);
// free_matrix(c.lin,1,2*c.n_elements,1,2*c.n_elements);
free_matrix(c.inverse,1,2*c.n_elements,1,2*c.n_elements);
for(x=1;x<=c.n_elements;x++)
{
c.I[x]=0.0;
c.V[x]=0.0;
c.phi[x]=0.0;
c.I_p[x]=0.0;
c.V_p[x]=0.0;
c.phi_p[x]=0.0;
c.I_pp[x]=0.0;
c.V_pp[x]=0.0;
c.phi_pp[x]=0.0;
c.I_int[x]=0.0;
c.V_int[x]=0.0;
switch (c.elements[x].TYPE)
{
case TYPE_CS:
sprintf(str,"%s_%s_I0",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].I),str);
break;
case TYPE_PS:
sprintf(str,"%s_%s_Phi0",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].Phi),str);
break;
case TYPE_R:
sprintf(str,"%_s%s_R",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].R),str);
break;
case TYPE_JJ:
sprintf(str,"%s_%s_Ic",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].Ic),str);
break;
case TYPE_KAPPA_JJ:
sprintf(str,"%s_%s_Ic",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].Ic),str);
break;
case TYPE_C:
sprintf(str,"%s_%s_C",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].C),str);
break;
case TYPE_L:
sprintf(str,"%s_%s_L",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.elements[x].L),str);
break;
}
if (c.elements[x].TYPE!=TYPE_CON)
{
sprintf(str,"%s_%s_V",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.V[x]),str);
sprintf(str,"%s_%s_phi",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.phi[x]),str);
sprintf(str,"%s_%s_I",c.elements[x].BLOCK,c.elements[x].name);
c.INT->tie_to_double(&(c.I[x]),str);
}
}
cout << "Circuit summary:\n";
cout << "Number of elements: \t" << c.n_elements << "\n";
cout << "Number of nodes (-ground):\t" << (c.n_nodes-1) << "\n";
cout << "Number of meshes: \t" << c.n_meshes << "\n";
cout << "Number of equations: \t" << (c.n_elements+c.n_meshes+c.n_nodes-1) << "\n";
cout << "Matrix elements: \t" << ((c.n_elements+c.n_meshes+c.n_nodes-1)*(c.n_elements+c.n_meshes+c.n_nodes-1)) << "\n";
cout << "Nonzero matrix elements \t" << c.sparse_n << "\n";
/* update_linear_matrix(c.lin,c.vec);
ludcmp(c.lin,2*c.n_elements,c.indx,&d);
lubksb(c.lin,2*c.n_elements,c.indx,c.vec);
memcpy(&(c.phi[1]),&(c.vec[1+c.n_elements]),c.n_elements*sizeof(double));
memcpy(&(c.I[1]),&(c.vec[1]),c.n_elements*sizeof(double));
for(x=1;x<=2*c.n_elements;x++)
c.solution[x]=c.vec[x];
c.SOLVER=solver;*/
}
void CIRC_solve()
{
static int cnt=0;
// static double **temp=matrix(1,2*c.n_elements,1,2*c.n_elements);
int n=2*c.n_elements;
double d=0.0;
double t_sol=0.0;
//The following algorithm uses the linearized version of the equations of motions and solves them directly by LU-decomposition.
//Possible speedup through matrix manipulation???
int iter=0;
double err=0;
int x,y,z;
update_linear_matrix(c.lin,c.vec);
if (c.SOLVER==SOLVER_LU)
{
ludcmp(c.lin,n,c.indx,&d);
lubksb(c.lin,n,c.indx,c.vec);
memcpy(&(c.phi[1]),&(c.vec[1+c.n_elements]),c.n_elements*sizeof(double));
memcpy(&(c.I[1]),&(c.vec[1]),c.n_elements*sizeof(double));
}
else if (c.SOLVER==SOLVER_BILIN)
{
linbcg_single(2*c.n_elements,c.vec,c.solution,1,1e-6,0,&iter,&err,c.sparse_vec,c.sparse_ind,c.inverse_vec,c.inverse_ind);
// dsprsax(c.inverse_vec,c.inverse_ind,c.vec,c.solution,2*c.n_elements);
memcpy(&(c.phi[1]),&(c.solution[1+c.n_elements]),c.n_elements*sizeof(double));
memcpy(&(c.I[1]),&(c.solution[1]),c.n_elements*sizeof(double));
}
else if (c.SOLVER==SOLVER_NEWTON)
{
mnewt(100,c.vec,c.n_elements*2,1e-6,MAX_ERROR,usrfun);
memcpy(&(c.phi[1]),&(c.vec[1+c.n_elements]),c.n_elements*sizeof(double));
memcpy(&(c.I[1]),&(c.vec[1]),c.n_elements*sizeof(double));
}
}
//Currents first
double element_deriv(int element,int var)
{
switch (c.elements[element].TYPE)
{
case TYPE_R:
if (var<=c.n_elements)//This is a current
if (element==var)
return -c.elements[element].R;
if (var>c.n_elements)//This is a phase
if (element==var-c.n_elements)
return 1.0/c.STEPT;
break;
case TYPE_L:
if (var<=c.n_elements)
if (element==var)
return c.elements[element].L;
if (var>c.n_elements)
if (element==var-c.n_elements)
return -1.0;
break;
case TYPE_C:
//This is a capacitor with the relation C_n=Q_n/U_n and Q=\Delta t\cdot\sum\limits_{i=0}^t I_n^t. Therefore, we have
if (var<=c.n_elements)//This is a current
if (element==var)
return 1.0;
if (var>c.n_elements)//This is a phase.
if (element==var-c.n_elements)
return -c.elements[element].C/c.STEPT/c.STEPT;
break;
case TYPE_JJ://Josephson Junction.
if (var<=c.n_elements)//This is a current
if (element==var)
return -1.0;
if (var>c.n_elements)//This is a phase
if (element==var-c.n_elements)
return c.elements[element].Ic*cos(c.phi[element])+c.elements[element].alpha/c.STEPT+1.0/c.STEPT/c.STEPT;
break;
case TYPE_KAPPA_JJ://kappa-Josephson Junction.
if (var<=c.n_elements)//This is a current
if (element==var)
return -1.0;
if (var>c.n_elements)//This is a phase
if (element==var-c.n_elements)
return c.elements[element].Ic*cos(c.phi[element]+c.elements[element].kappa*PI)+c.elements[element].alpha/c.STEPT+1.0/c.STEPT/c.STEPT;
break;
case TYPE_CS:
if (var<=c.n_elements)//this is a current source
if (element==var)
return 1.0;
break;
case TYPE_NC:
if (var<=c.n_elements)//this is a current source
if (element==var)
return 1.0;
break;
case TYPE_CSG:
if (var<=c.n_elements)//this is a current signal generator
if (element==var)
return 1.0;
break;
case TYPE_VSG:
if (var>c.n_elements)//this is a voltage signal generator
if (element==var-c.n_elements)
return 1.0/c.STEPT;
break;
case TYPE_PSG:
if (var>c.n_elements)//this is a voltage signal generator
if (element==var-c.n_elements)
return 1.0;
break;
case TYPE_PS://This is a phase source.
if (var>c.n_elements)
if (element==var-c.n_elements)
return 1.0;
break;
case TYPE_VS:
if (var>c.n_elements)//this is a voltage source
if (element==var-c.n_elements)
return 1.0/c.STEPT;
break;
}
return 0;
}
//Currents first, then phases.
double mesh_deriv(int mesh,int var)
{
for(int x=0;x<c.meshes[mesh].n_elements;x++)
if (abs(c.meshes[mesh].elements[x])==var-c.n_elements)
return c.meshes[mesh].elements[x]>0.0?1.0:-1.0;
return 0;
}
double node_deriv(int node,int var)
{
for(int x=0;x<c.nodes[node].n_elements;x++)
if (abs(c.nodes[node].elements[x])==var)
return c.nodes[node].elements[x]>0.0?1.0:-1.0;
return 0;
}
//Returns the right-handed side of an element equation, i.e. the constant part of it.
double rhs(int element)
{
switch(c.elements[element].TYPE)
{
case TYPE_R:
return c.phi_p[element]/c.STEPT;
break;
case TYPE_CON:
return 0;
break;
case TYPE_L:
return 0.0;
break;
case TYPE_C:
return c.elements[element].C*(2.*c.phi_p[element]-c.phi_pp[element])/c.STEPT/c.STEPT;
break;
case TYPE_JJ://Josephson Junction.
return -c.elements[element].Ic*sin(c.phi_p[element])+c.elements[element].Ic*c.phi_p[element]*cos(c.phi_p[element])+c.elements[element].alpha/c.STEPT*c.phi_p[element]+2.0/c.STEPT/c.STEPT*c.phi_p[element]-c.phi_pp[element]/c.STEPT/c.STEPT;
break;
case TYPE_KAPPA_JJ://kappa-Josephson Junction.
return -c.elements[element].Ic*sin(c.phi_p[element]+c.elements[element].kappa*PI)+c.elements[element].Ic*c.phi_p[element]*cos(c.phi_p[element]+c.elements[element].kappa*PI)+c.elements[element].alpha/c.STEPT*c.phi_p[element]+2.0/c.STEPT/c.STEPT*c.phi_p[element]-c.phi_pp[element]/c.STEPT/c.STEPT;
break;
case TYPE_CS:
return c.elements[element].I;
break;
case TYPE_NC:
return c.elements[element].I;
break;
case TYPE_CSG:
return SG(c.elements[element]);
break;
case TYPE_VSG:
return SG(c.elements[element])+c.phi_p[element]/c.STEPT;
break;
case TYPE_PSG:
return SG(c.elements[element]);
break;
case TYPE_PS://This is a phase source.
return c.elements[element].Phi;
break;
case TYPE_VS:
return c.elements[element].V+c.phi_p[element]/c.STEPT;
break;
}
return 0;
}
//Returns the linear coefficient for a certain element, i.e. the left-handed coefficient in the linearized element model.
double linear_coeff(int element,int var,double lin)
{
switch (c.elements[element].TYPE)
{
case TYPE_R:
if (var<=c.n_elements)//This is a current
if (element==var)
return -c.elements[element].R;
if (var>c.n_elements)//This is a phase
if (element==var-c.n_elements)
return 1.0/c.STEPT;
break;
case TYPE_L:
if (var<=c.n_elements)
if (element==var)
return c.elements[element].L;
if (var>c.n_elements)
if (element==var-c.n_elements)
return -1.0;
break;
case TYPE_C:
//This is a capacitor with the relation C_n=Q_n/U_n and Q=\Delta t\cdot\sum\limits_{i=0}^t I_n^t. Therefore, we have
if (var<=c.n_elements)//This is a current
if (element==var)
return -1.0;
if (var>c.n_elements)//This is a phase.
if (element==var-c.n_elements)
return c.elements[element].C/c.STEPT/c.STEPT;
break;
case TYPE_JJ://Josephson Junction.
if (var<=c.n_elements)//This is a current
if (element==var)
return -1.0;
if (var>c.n_elements)//This is a phase
if (element==var-c.n_elements)
return c.elements[element].Ic*cos(c.phi_p[element])*lin+c.elements[element].alpha/c.STEPT+1.0/c.STEPT/c.STEPT;
break;
case TYPE_KAPPA_JJ://Josephson Junction.
if (var<=c.n_elements)//This is a current
if (element==var)
return -1.0;
if (var>c.n_elements)//This is a phase
if (element==var-c.n_elements)
return c.elements[element].Ic*cos(c.phi_p[element]+c.elements[element].kappa*PI)*lin+c.elements[element].alpha/c.STEPT+1.0/c.STEPT/c.STEPT;
break;
case TYPE_CS:
if (var<=c.n_elements)//this is a current source
if (element==var)
return 1.0;
break;
case TYPE_NC:
if (var<=c.n_elements)//this is a current source
if (element==var)
return 1.0;
break;
case TYPE_CSG:
if (var<=c.n_elements)//this is a current signal generator
if (element==var)
return 1.0;
break;
case TYPE_VSG:
if (var>c.n_elements)//this is a voltage signal generator
if (element==var-c.n_elements)
return 1.0/c.STEPT;
break;
case TYPE_PSG:
if (var>c.n_elements)//this is a voltage signal generator
if (element==var-c.n_elements)
return 1.0;
break;
case TYPE_PS://This is a phase source.
if (var>c.n_elements)
if (element==var-c.n_elements)
return 1.0;
break;
case TYPE_VS:
if (var>c.n_elements)//this is a voltage source
if (element==var-c.n_elements)
return 1.0/c.STEPT;
break;
}
return 0;
}
//Updates the linear matrix obtained by "generate_linear_matrix". Only dynamic coefficients are changed.
void update_linear_matrix(double **lin,double *vec)
{
int x,element;
//First, we restore the original version of the matrix...
if (c.SOLVER!=SOLVER_BILIN)
{
for(x=1;x<=2*c.n_elements;x++)
memcpy(&(c.lin[x][1]),&(c.lin_save[x][1]),(2*(c.n_elements))*sizeof(double));
memcpy(&(c.vec[1]),&(c.vec_save[1]),(2*c.n_elements)*sizeof(double));
}
//Then, we repopulate the element equations in the matrix and the solution vector.
for(x=c.n_elements+1;x<=2*c.n_elements;x++)
{
if (c.SOLVER!=SOLVER_BILIN)
{
lin[x][x]=linear_coeff(x-c.n_elements,x,1.0);
}
vec[x]=rhs(x-c.n_elements);
c.sparse_vec[c.map[x][x]]=linear_coeff(x-c.n_elements,x,1.0);
}
}
//Returns the linearized matrix of the system, together with the rhs vector.
int generate_linear_matrix(double **lin,double *vec,double set_lin)
{
int n=1;
for(int x=1;x<=2*c.n_elements;x++)
{
for(int y=1;y<=2*c.n_elements;y++)
{
if (x>c.n_elements)
{
if ((x-c.n_elements==y) || (x==y))//This is a small shortcut to improve calculation time...
{
lin[x][y]=linear_coeff(x-c.n_elements,y,set_lin);
}
else
lin[x][y]=0.0;
if ((x-c.n_elements)==y)
{
c.current_matrix[x-c.n_elements][y]=lin[x][y];
}
else if (x==y)
{
c.voltage_matrix[x-c.n_elements][y-c.n_elements]=lin[x][y];
}
else {if (y>c.n_elements)
{
c.voltage_matrix[x-c.n_elements][y-c.n_elements]=0.0;
c.inv_voltage_matrix[x-c.n_elements][y-c.n_elements]=0.0;
}
else
{
c.inv_current_matrix[x-c.n_elements][y]=0.0;
c.current_matrix[x-c.n_elements][y]=0.0;
} }
//This is an element equation.
}
else if (x>c.n_meshes)
{
if (y<=c.n_elements)
lin[x][y]=c.node_matrix[x-c.n_meshes][y];//mesh_deriv(x-1,y);
else
lin[x][y]=0.0;
// lin[x][y]=node_deriv(x-c.n_meshes-1,y);
//This is a node equation.
}
else
{
if (y>c.n_elements)
{
lin[x][y]=c.mesh_matrix[x][y-c.n_elements];//mesh_deriv(x-1,y);
}
else
{
lin[x][y]=0.0;
}
//This is a mesh equation.
}
if (fabs(lin[x][y])>=THLIN/10.0)
n++;
}
if (x>c.n_elements)
{
//This is an element vector
vec[x]=rhs(x-c.n_elements);
}
else
{
vec[x]=0.0;
}
}
cout << "N:" << n << "\n";
return n;
}
void init_jacobian(double **fjac)
{
for(int x=1;x<=2*c.n_elements;x++)
for(int y=1;y<=2*c.n_elements;y++)
{
if (x>=c.n_meshes+c.n_nodes)
{
if ((x-c.n_meshes-c.n_nodes+1==y) || (x-c.n_meshes-c.n_nodes+1+c.n_elements==y))//This is a small shortcut to improve calculation time...
fjac[x][y]=element_deriv(x-c.n_meshes-c.n_nodes+1,y);
else
fjac[x][y]=0.0;
//This is an element equation.
}
else if (x>c.n_meshes)
{
fjac[x][y]=node_deriv(x-c.n_meshes-1,y);
//This is a node equation.
}
else
{
fjac[x][y]=mesh_deriv(x-1,y);
//This is a mesh equation.
}
}
}
void generate_jacobian(double **fjac)
{
for(int x=c.n_meshes+c.n_nodes;x<=2*c.n_elements;x++)
{
fjac[x][x-c.n_meshes-c.n_nodes+1]=element_deriv(x-c.n_meshes-c.n_nodes+1,x-c.n_meshes-c.n_nodes+1);
fjac[x][x-c.n_meshes-c.n_nodes+1+c.n_elements]=element_deriv(x-c.n_meshes-c.n_nodes+1,x-c.n_meshes-c.n_nodes+1+c.n_elements);
}
}
//Returns the element (x,y) of the Jacobian matrix of the system.
double JJ(int x,int y)
{
//Row select
if (x>=c.n_meshes+c.n_nodes)
{
if ((x-c.n_meshes-c.n_nodes+1==y) || (x-c.n_meshes-c.n_nodes+1+c.n_elements==y))//This is a small shortcut to improve calculation time...
return element_deriv(x-c.n_meshes-c.n_nodes+1,y);
else
return 0.0;
//This is an element equation.
}
else if (x>c.n_meshes)
{
return node_deriv(x-c.n_meshes-1,y);
//This is a node equation.
}
else
{
return mesh_deriv(x-1,y);
//This is a mesh equation.
}
return 0.0;
}
double error_func()
{
double error=0.0;
for(int x=1;x<=2*c.n_elements;x++)
{
error+=fabs(func(x));
}
return error;
}
//Pushes a solution to the stack.
void push_solution(void)
{
if (STACK_LENGTH==-1)
{
STACK=(STACK_ELEMENT *)malloc(sizeof(STACK_ELEMENT));
STACK_LENGTH=0;
}
STACK_LENGTH++;
STACK=(STACK_ELEMENT *)realloc((void *)STACK,sizeof(STACK_ELEMENT)*STACK_LENGTH);
STACK[STACK_LENGTH-1].I=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].V=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].phi=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].I_int=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].V_int=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].I_p=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].V_p=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].phi_p=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].I_pp=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].V_pp=(double *)malloc(sizeof(double)*(c.n_elements+1));
STACK[STACK_LENGTH-1].phi_pp=(double *)malloc(sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].I_int,(void *)c.I_int,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].V_int,(void *)c.V_int,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].I,(void *)c.I,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].I_p,(void *)c.I_p,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].I_pp,(void *)c.I_pp,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].V,(void *)c.V,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].V_p,(void *)c.V_p,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].V_pp,(void *)c.V_pp,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].phi,(void *)c.phi,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].phi_p,(void *)c.phi_p,sizeof(double)*(c.n_elements+1));
memcpy((void *)STACK[STACK_LENGTH-1].phi_pp,(void *)c.phi_pp,sizeof(double)*(c.n_elements+1));
}
//Pops a solution from the stack.
void pop_solution(void)
{
if (STACK_LENGTH>0)
{
STACK_LENGTH--;
free(c.phi);
free(c.phi_p);
free(c.phi_pp);
free(c.I_int);
free(c.V_int);
free(c.I_p);
free(c.V_p);
free(c.I_pp);
free(c.V_pp);
c.phi=STACK[STACK_LENGTH].phi;
c.phi_p=STACK[STACK_LENGTH].phi_p;
c.phi_pp=STACK[STACK_LENGTH].phi_pp;
c.I_int=STACK[STACK_LENGTH].I_int;
c.V_int=STACK[STACK_LENGTH].V_int;
c.V_p=STACK[STACK_LENGTH].V_p;
c.I_p=STACK[STACK_LENGTH].I_p;
c.V_pp=STACK[STACK_LENGTH].V_pp;
c.I_pp=STACK[STACK_LENGTH].I_pp;
STACK=(STACK_ELEMENT *)realloc((void *)STACK,sizeof(STACK_ELEMENT)*(STACK_LENGTH>0?STACK_LENGTH:1));
}
}
//Returns the function value for row "x".
double func(int x)