-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
1275 lines (948 loc) · 42.1 KB
/
main.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 <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <complex>
#include "library/Eigen/Dense"
#include <sys/time.h>
using namespace Eigen;
typedef unsigned long long timemarker;
static timemarker get_timestamp (){
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timemarker)now.tv_sec * 1000000;
}
//define parsing structure for two terminal components
struct NodePoint{
int x;
int y;
};
//define parsing structure for three terminal componenets
struct NodeTri{
int x;
int y;
int z;
};
//class for impedance decives (Resistor, Capacitor, Inductor)
class ImpedanceDevice{
public:
virtual std::string show_nodeinfo() const = 0;
virtual std::complex<double> get_impedance(double omega) const = 0;
virtual std::complex<double> get_conductance(double omega) const = 0;
virtual NodePoint give_nodeinfo() const = 0;
virtual ~ImpedanceDevice() { }
};
//inheritance class for resistor
class Resistor : public ImpedanceDevice{
public:
Resistor(int n1, int n2, double r) : node1(n1), node2(n2), resistance(r) { }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node1) + ", " + std::to_string(node2) + ")";
}
std::complex<double> get_impedance(double omega) const {
std::complex<double> impedance(resistance,0);
return impedance;
}
std::complex<double> get_conductance(double omega) const {
std::complex<double> conductance(1/resistance,0);
return conductance;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node1;
N.y = node2;
return N;
}
private:
int node1;
int node2;
double resistance;
};
//inheritance class for capacittor
class Capacitor : public ImpedanceDevice{
public:
Capacitor(int n1, int n2, double c) : node1(n1), node2(n2), capacitance(c) { }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node1) + ", " + std::to_string(node2) + ")";
}
std::complex<double>get_impedance(double omega) const {
std::complex<double> impedance(0, - 1/(omega * capacitance));
return impedance;
}
std::complex<double> get_conductance(double omega) const {
std::complex<double> conductance(0, omega * capacitance);
return conductance;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node1;
N.y = node2;
return N;
}
private :
int node1;
int node2;
double capacitance;
};
//inheritance class for inductor
class Inductor : public ImpedanceDevice{
public:
Inductor(int n1, int n2, double l) : node1(n1), node2(n2), inductance(l) { }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node1) + ", " + std::to_string(node2) + ")";
}
std::complex<double>get_impedance(double omega) const {
std::complex<double> impedance(0, (omega * inductance));
return impedance;
}
std::complex<double> get_conductance(double omega) const {
std::complex<double> conductance(0, -1/(omega * inductance));
return conductance;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node1;
N.y = node2;
return N;
}
private:
int node1;
int node2;
double inductance;
};
//class for sources (DC Voltage, DC Current, AC Voltage, AC Current, Voltage Controlled Current)
class Source{
public:
virtual std::string show_nodeinfo() const = 0;
virtual double get_magnitude() const {
return 0;
}
virtual double get_phase() const {
return 0;
}
virtual double get_gm() const {
return 0;
}
virtual std::string get_type() const = 0;
virtual std::string get_source_label() const = 0;
virtual NodePoint give_nodeinfo() const = 0;
virtual NodePoint give_controlinfo() const {
NodePoint C;
C.x = 0;
C.y = 0;
return C;
}
virtual ~Source() { }
};
//inheritance class for DC Voltage Source
class DCVSource : public Source{
public:
DCVSource(int n_p, int n_m, double v, std::string label) : node_plus(n_p), node_minus(n_m), voltage(v), source_label(label){ }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_plus) + ", " + std::to_string(node_minus) + ")";
}
double get_magnitude() const {
return voltage;
}
std::string get_type() const {
return "DC V";
}
std::string get_source_label() const {
return source_label;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node_plus;
N.y = node_minus;
return N;
}
private:
int node_plus;
int node_minus;
double voltage;
std::string source_label;
};
//inheritance class for DC current Source
class DCISource : public Source{
public:
DCISource(int n_in, int n_out, double i, std::string label) : node_in(n_in), node_out(n_out), current(i), source_label(label){ }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_in) + ", " + std::to_string(node_out) + ")";
}
double get_magnitude() const {
return current;
}
std::string get_type() const {
return "DC I";
}
std::string get_source_label() const {
return source_label;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node_in;
N.y = node_out;
return N;
}
private:
int node_in;
int node_out;
double current;
std::string source_label;
};
//inheritance class for AC Voltage Source
class ACVSource : public Source{
public:
ACVSource(int n_p, int n_m, double v_m, double v_p, std::string label) : node_plus(n_p), node_minus(n_m), amplitude(v_m), phase(v_p), source_label(label){ }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_plus) + ", " + std::to_string(node_minus) + ")";
}
double get_magnitude() const {
return amplitude;
}
double get_phase() const {
return phase;
}
std::string get_type() const {
return "AC V";
}
std::string get_source_label() const {
return source_label;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node_plus;
N.y = node_minus;
return N;
}
private:
int node_plus;
int node_minus;
double amplitude;
double phase;
std::string source_label;
};
//inheritance class for AC Current Source
class ACISource : public Source{
public:
ACISource(int n_in, int n_out, double i_m, double i_p, std::string label) : node_in(n_in), node_out(n_out), amplitude(i_m), phase(i_p), source_label(label){ }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_in) + ", " + std::to_string(node_out) + ")";
}
double get_magnitude() const {
return amplitude;
}
double get_phase() const {
return phase;
}
std::string get_type() const {
return "AC I";
}
std::string get_source_label() const {
return source_label;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node_in;
N.y = node_out;
return N;
}
private:
int node_in;
int node_out;
double amplitude;
double phase;
std::string source_label;
};
//inheritance class for Voltage Controlled Current Source
class VCCSource : public Source{
public:
VCCSource(int n_p, int n_m, int c_p, int c_m, double g_m, std::string label) : node_plus(n_p), node_minus(n_m), control_plus(c_p), control_minus(c_m), transconductance(g_m), source_label(label){ }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_plus) + ", " + std::to_string(node_minus) + ")";
}
double get_gm() const {
return transconductance;
}
std::string get_type() const {
return "VCCS";
}
std::string get_source_label() const {
return source_label;
}
NodePoint give_nodeinfo() const {
NodePoint N;
N.x = node_plus;
N.y = node_minus;
return N;
}
NodePoint give_controlinfo() const {
NodePoint C;
C.x = control_plus;
C.y = control_minus;
return C;
}
private:
int node_plus;
int node_minus;
int control_plus;
int control_minus;
double transconductance;
std::string source_label;
};
//class for non-linear devices (diode, bjt, mosfet)
class NonLinearDevice{
public:
virtual std::string show_nodeinfo() const = 0;
virtual NodePoint give_binodeinfo() const {
NodePoint N;
N.x = 0;
N.y = 0;
return N;
}
virtual NodeTri give_trinodeinfo() const {
NodeTri N;
N.x = 0;
N.y = 0;
N.z = 0;
return N;
}
virtual std::string get_model() const = 0;
virtual ~NonLinearDevice() { }
};
//inheritance class for diode
class Diode: public NonLinearDevice{
public:
Diode(int n_a, int n_c, std::string mod) : node_an(n_a), node_cat(n_c), model(mod){ }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_an) + ", " + std::to_string(node_cat) +")";
}
NodePoint give_binodeinfo() const {
NodePoint N;
N.x = node_an;
N.y = node_cat;
return N;
}
std::string get_model() const {
return model;
}
private:
int node_an;
int node_cat;
std::string model;
};
//inheritance class for bjt
class BJT: public NonLinearDevice{
public:
BJT(int n_c, int n_b, int n_e, std::string mod) : node_c(n_c), node_b(n_b), node_e(n_e), model(mod) { }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_c) + ", " + std::to_string(node_b) + ", " + std::to_string(node_e) +")";
}
NodeTri give_trinodeinfo() const {
NodeTri N;
N.x = node_c;
N.y = node_b;
N.z = node_e;
return N;
}
std::string get_model() const {
return model;
}
private:
int node_c;
int node_b;
int node_e;
std::string model;
};
//inheritance class for mosfet
class MOSFET: public NonLinearDevice{
public:
MOSFET(int n_d, int n_g, int n_s, std::string mod) : node_d(n_d), node_g(n_g), node_s(n_s), model(mod) { }
std::string show_nodeinfo() const {
return "Nodal Coordinates: (" + std::to_string(node_d) + ", " + std::to_string(node_g) + ", " + std::to_string(node_s) + ")";
}
NodeTri give_trinodeinfo() const {
NodeTri N;
N.x = node_d;
N.y = node_g;
N.z = node_s;
return N;
}
std::string get_model() const {
return model;
}
private:
int node_d;
int node_g;
int node_s;
std::string model;
};
//ignores non-numeric characters in value part and extract the magnitude
double extract_double(const std::string& label){
std::string double_string;
for(int i = 0; i < label.size(); i++){
if(std::isdigit(label[i]) || label[i] == '.' || label[i] == '-'){
double_string.push_back(label[i]);
}
}
return std::stod(double_string);
}
//change input string node into number
int node_to_number(const std::string& node){
int node_number;
double node_double;
node_double = extract_double(node);
node_number = (int) node_double;
return node_number;
}
//convert prefix into value
double prefix_convertor(const std::string& value){
std::string prefix;
double num_quantity;
//loop needed as not all prefixes are 1 character long
for(int i = 0; i < value.size(); i++){
if(std::isdigit(value[i]) == false && value[i] != '.' && value[i] != '-' && value[i] != '(' && value[i] != 'A' && value[i] != 'C'){
prefix.push_back(value[i]);
}
}
num_quantity = extract_double(value);
if(prefix == "p"){
num_quantity = num_quantity * pow(10, -12);
}
else if(prefix == "n"){
num_quantity = num_quantity * pow(10, -9);
}
else if(prefix == "\u00B5"){
num_quantity = num_quantity * pow(10, -6);
}
else if(prefix == "m"){
num_quantity = num_quantity * pow(10, -3);
}
else if(prefix == "k"){
num_quantity = num_quantity * pow(10, 3);
}
else if(prefix == "Meg"){
num_quantity = num_quantity * pow(10, 6);
}
else if(prefix == "G"){
num_quantity = num_quantity * pow(10, 9);
}
else{
return num_quantity;
}
return num_quantity;
}
//find the number of nodes in the circuit
// can limit to three node inputs because VCCS control nodes are presumably connected to other components present in the circuit that will be considered
int max_node_number(int node_max, const int& node1, const int& node2, const int& node3){
if((node1 > node_max) && (node1 >= node2) && (node1 >= node3)){
return node1;
}
else if((node2 > node_max) && (node2 >= node1) && (node2 >= node3)){
return node2;
}
else if((node3 > node_max) && (node3 >= node1) && (node3 >= node2)){
return node3;
}
return node_max;
}
bool detect_input_source(const std::vector<Source*>& sources, const std::string& inputsource_label){
for(int i = 0; i < sources.size(); i++){
if(sources[i]->get_source_label() == inputsource_label){
return true;
}
}
return false;
}
//calculate the magnitude of the transfer function
double return_tf_magnitude(const std::complex<double>& source, const std::complex<double>& output_node){
double gain;
gain = std::abs(output_node/source);
return gain;
}
//calculate the phase of the transfer function
double return_tf_phase(const std::complex<double>& source, const std::complex<double>& output_node){
double phase_change;
phase_change = (std::arg(output_node) - std::arg(source)) * 180 / M_PI;
return phase_change;
}
//construct conductanc matrix only with conductances (ignoring source rows)
MatrixXcd cons_conductance_matrix(MatrixXcd A, const std::vector<ImpedanceDevice*>& impedances, const double& omega){
for(int i = 0; i < impedances.size(); i++){
if(impedances[i]->give_nodeinfo().x != 0 && impedances[i]->give_nodeinfo().y != 0){
A(impedances[i]->give_nodeinfo().x - 1, impedances[i]->give_nodeinfo().y - 1) = A(impedances[i]->give_nodeinfo().x - 1, impedances[i]->give_nodeinfo().y - 1) - impedances[i]->get_conductance(omega);
A(impedances[i]->give_nodeinfo().y - 1, impedances[i]->give_nodeinfo().x - 1) = A(impedances[i]->give_nodeinfo().y - 1, impedances[i]->give_nodeinfo().x - 1) - impedances[i]->get_conductance(omega);
A(impedances[i]->give_nodeinfo().x - 1, impedances[i]->give_nodeinfo().x - 1) = A(impedances[i]->give_nodeinfo().x - 1, impedances[i]->give_nodeinfo().x - 1) + impedances[i]->get_conductance(omega);
A(impedances[i]->give_nodeinfo().y - 1, impedances[i]->give_nodeinfo().y - 1) = A(impedances[i]->give_nodeinfo().y - 1, impedances[i]->give_nodeinfo().y - 1) + impedances[i]->get_conductance(omega);
}
else if(impedances[i]->give_nodeinfo().x == 0 && impedances[i]->give_nodeinfo().y != 0){
A(impedances[i]->give_nodeinfo().y - 1, impedances[i]->give_nodeinfo().y - 1) = A(impedances[i]->give_nodeinfo().y - 1, impedances[i]->give_nodeinfo().y - 1) + impedances[i]->get_conductance(omega);
}
else if(impedances[i]->give_nodeinfo().x != 0 && impedances[i]->give_nodeinfo().y == 0){
A(impedances[i]->give_nodeinfo().x - 1, impedances[i]->give_nodeinfo().x - 1) = A(impedances[i]->give_nodeinfo().x - 1, impedances[i]->give_nodeinfo().x - 1) + impedances[i]->get_conductance(omega);
}
}
return A;
}
//considers both grounded and floating current sources
void isource_analysis(MatrixXcd& Bref, Source* source_i, const std::complex<double>& isource){
std::complex<double> negative(-1,0);
if(source_i->give_nodeinfo().x != 0){
Bref(source_i->give_nodeinfo().x - 1,0) = negative * isource;
//negative due to orientation of current source
}
if(source_i->give_nodeinfo().y != 0){
Bref(source_i->give_nodeinfo().y - 1,0) = isource;
}
}
//considers both grounded and floating VCCS
void vccsource_analysis(MatrixXcd& A, Source* source_vcc, const std::complex<double>& gm){
if(source_vcc->give_nodeinfo().x != 0){
A(source_vcc->give_nodeinfo().x - 1, source_vcc->give_controlinfo().x - 1) = A(source_vcc->give_nodeinfo().x - 1, source_vcc->give_controlinfo().x - 1) + gm;
A(source_vcc->give_nodeinfo().x - 1, source_vcc->give_controlinfo().y - 1) = A(source_vcc->give_nodeinfo().x - 1, source_vcc->give_controlinfo().y - 1) - gm;
}
if(source_vcc->give_nodeinfo().y != 0){
A(source_vcc->give_nodeinfo().y - 1, source_vcc->give_controlinfo().x - 1) = A(source_vcc->give_nodeinfo().y - 1, source_vcc->give_controlinfo().x - 1) - gm;
A(source_vcc->give_nodeinfo().y - 1, source_vcc->give_controlinfo().y - 1) = A(source_vcc->give_nodeinfo().y - 1, source_vcc->give_controlinfo().y - 1) + gm;
}
}
//forms supernode row by adding the rows of the 2 nodes that form the supernode
void fvsource_analysis1(MatrixXcd& A, MatrixXcd& B, const MatrixXcd& G, const MatrixXcd& Bref, Source* source_fv, const int& n_max){
for(int k = 0; k < n_max; k++){
A(source_fv->give_nodeinfo().y - 1, k) = G(source_fv->give_nodeinfo().y - 1, k) + G(source_fv->give_nodeinfo().x - 1, k);
}
B(source_fv->give_nodeinfo().y - 1, 0) = Bref(source_fv->give_nodeinfo().y - 1, 0) + Bref(source_fv->give_nodeinfo().x - 1, 0);
}
//inserts 0,1,-1 in A and Vsrc in B
void fvsource_analysis2(MatrixXcd& A, MatrixXcd& B, Source* source_fv, const std::complex<double>& fvsource, const int& n_max){
std::complex<double> zero(0,0), one(1,0), negative(-1,0);
//sets row representing floating source to all zero first
for(int k = 0; k < n_max; k++){
A(source_fv->give_nodeinfo().x - 1,k) = zero;
}
//inserts 1 and -1 into row representing voltage source
A(source_fv->give_nodeinfo().x - 1,source_fv->give_nodeinfo().x - 1) = one;
A(source_fv->give_nodeinfo().x - 1,source_fv->give_nodeinfo().y - 1) = negative;
//sets correct entry of B matrix to represent the source
B(source_fv->give_nodeinfo().x - 1,0) = fvsource;
}
//inserts 0,1 in A and Vsrc or -Vsrc in B
void gvsource_analysis(MatrixXcd& A, MatrixXcd& B, Source* source_gv, const std::complex<double>& gvsource, const int& n_max){
std::complex<double> zero(0,0), one(1,0), negative(-1,0);
if(source_gv->give_nodeinfo().x != 0){
B(source_gv->give_nodeinfo().x - 1,0) = gvsource;
for(int k = 0; k < n_max; k++){
A(source_gv->give_nodeinfo().x - 1,k) = zero;
}
A(source_gv->give_nodeinfo().x - 1,source_gv->give_nodeinfo().x - 1) = one;
}
else{
B(source_gv->give_nodeinfo().y - 1,0) = negative * gvsource;
//account for polarity of voltage source
for(int k = 0; k < n_max; k++){
A(source_gv->give_nodeinfo().y - 1,k) = zero;
}
A(source_gv->give_nodeinfo().y - 1,source_gv->give_nodeinfo().y - 1) = one;
}
}
//resets matrices after every iteration of loop for DC and AC analysis
void reset_matrices(MatrixXcd& A, MatrixXcd& B, MatrixXcd& Bref, MatrixXcd& G, MatrixXcd& X, std::vector<ImpedanceDevice*> impedance_devices, double omega){
A.setZero();
A = cons_conductance_matrix(A, impedance_devices, omega);
B.setZero();
Bref.setZero();
G.setZero();
G = cons_conductance_matrix(G, impedance_devices, omega);
X.setZero();
}
int main(){
timemarker t0 = get_timestamp();
std::ifstream infile;
std::string input_file_name;
std::cout << "What is the name of the netlist input file?" << std::endl;
timemarker t1 = get_timestamp();
std::cin >> input_file_name;
timemarker t2 = get_timestamp();
infile.open(input_file_name);
if(!infile.is_open()){
std::cout << "error opening file" << std::endl;
return EXIT_FAILURE;
}
std::string component;
std::vector<std::string> substrs;
//can we delete impedance_devices and sources? not needed for analysis portion only needed to test parse
std::vector<ImpedanceDevice*> ss_impedance_devices, dc_impedance_devices;
ImpedanceDevice* tmp_id;
ImpedanceDevice* tmp_id2;
std::vector<Source*> ss_sources, dc_sources;
Source* tmp_s;
Source* tmp_s2;
//nonlinear values
std::vector<NonLinearDevice*> non_linear_devices;
NonLinearDevice* tmp_nld;
// frequency step parameters, we assume ac analysis always done in decades
double f_start, f_stop, n_ppd, f=0, omega = 0;
int n_max = 0;
while(std::getline(infile, component)){
std::stringstream line(component);
while(line.good()){
std::string substr;
std::getline(line, substr, ' ');
substrs.push_back(substr);
}
if(substrs[0][0] == 'R'){
tmp_id = new Resistor(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]));
ss_impedance_devices.push_back(tmp_id);
dc_impedance_devices.push_back(tmp_id);//new
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'C'){
tmp_id = new Capacitor(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]));
ss_impedance_devices.push_back(tmp_id);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'L'){
tmp_id = new Inductor(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]));
tmp_id2 = new Resistor(node_to_number(substrs[1]), node_to_number(substrs[2]), 0.001);
ss_impedance_devices.push_back(tmp_id);
dc_impedance_devices.push_back(tmp_id2);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'V' && substrs[3][0] != 'A'){
tmp_s = new DCVSource(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]), substrs[0]);
tmp_id = new Resistor(node_to_number(substrs[1]), node_to_number(substrs[2]), 0.001);
//ss equivalent of DC Voltage Source is short circuit, represented by resistor of 1m
dc_sources.push_back(tmp_s);//new
ss_impedance_devices.push_back(tmp_id);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'V' && substrs[3][0] == 'A'){
tmp_s = new ACVSource(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]), extract_double(substrs[4]), substrs[0]);
tmp_id = new Resistor(node_to_number(substrs[1]), node_to_number(substrs[2]), 0.001);
ss_sources.push_back(tmp_s);
dc_impedance_devices.push_back(tmp_id);//new
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'I' && substrs[3][0] != 'A'){
tmp_s = new DCISource(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]), substrs[0]);
dc_sources.push_back(tmp_s);//new
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'I' && substrs[3][0] == 'A'){
tmp_s = new ACISource(node_to_number(substrs[1]), node_to_number(substrs[2]), prefix_convertor(substrs[3]), extract_double(substrs[4]), substrs[0]);
ss_sources.push_back(tmp_s);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'G'){
tmp_s = new VCCSource(node_to_number(substrs[1]), node_to_number(substrs[2]), node_to_number(substrs[3]), node_to_number(substrs[4]), prefix_convertor(substrs[5]), substrs[0]);
ss_sources.push_back(tmp_s);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'D'){
tmp_nld = new Diode(node_to_number(substrs[1]), node_to_number(substrs[2]), substrs[3]);
non_linear_devices.push_back(tmp_nld);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), 0);
}
else if(substrs[0][0] == 'Q'){
tmp_nld = new BJT(node_to_number(substrs[1]), node_to_number(substrs[2]), node_to_number(substrs[3]), substrs[4]);
non_linear_devices.push_back(tmp_nld);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), node_to_number(substrs[3]));
}
else if(substrs[0][0] == 'M'){
tmp_nld = new MOSFET(node_to_number(substrs[1]), node_to_number(substrs[2]), node_to_number(substrs[3]), substrs[4]);
non_linear_devices.push_back(tmp_nld);
n_max = max_node_number(n_max, node_to_number(substrs[1]), node_to_number(substrs[2]), node_to_number(substrs[3]));
}
else if(substrs[0] == ".ac"){
n_ppd = prefix_convertor(substrs[2]);
f_start = prefix_convertor(substrs[3]);
f_stop = prefix_convertor(substrs[4]);
}
else if(substrs[0] == ".end"){
infile.close();
}
substrs.clear();
}
infile.close();
MatrixXcd matrixA(n_max,n_max), matrixG(n_max, n_max), matrixB(n_max, 1), matrixBref(n_max, 1), matrixX(n_max, 1);
std::complex<double> InputSource(0,0);
int n_output;
std::string s_input;
std::cout << "Which node is the output node?" << std::endl;
timemarker t3 = get_timestamp();
std::cin >> n_output;
if((n_output > n_max) || (n_output <= 0)){
std::cout << "error, invalid output node" << std::endl;
return EXIT_FAILURE;
}
timemarker t4 = get_timestamp();
std::cout << "Which source is the input source?" << std::endl;
timemarker t5 = get_timestamp();
std::cin >> s_input;
if(detect_input_source(ss_sources, s_input) == false){
std::cout << "error, nominated input source does not exist" << std::endl;
return EXIT_FAILURE;
}
timemarker t6 = get_timestamp();
for(int i=0; i<non_linear_devices.size(); i++){
double Geq, Ieq, Vd = 0.7, Id, Is_diode = 1 * pow(10, -14), Is_bjt = 1 * pow(10,-16), Vt = 25.865 * pow(10, -3), V1 = 0, V2 = 0, Vdlast = 1, beta = 100, Kp = 2 * pow(10,-5);
while(std::abs(Vdlast - Vd)>=0.00000001){
Vdlast = Vd;
if(non_linear_devices[i]->get_model() == "D"){
Id = Is_diode * (exp(Vd/Vt)-1);
Geq = Is_diode/Vt * exp(Vd/Vt);
Ieq = Id - Geq* Vd;
tmp_s= new DCISource(non_linear_devices[i]->give_binodeinfo().x, non_linear_devices[0]->give_binodeinfo().y, Ieq, "NA" );
tmp_id = new Resistor(non_linear_devices[i]->give_binodeinfo().x, non_linear_devices[0]->give_binodeinfo().y, 1/Geq);
dc_sources.push_back(tmp_s);
dc_impedance_devices.push_back(tmp_id);
}
else if(non_linear_devices[i]->get_model() == "NPN"){
Id = Is_bjt/beta * (exp(Vd/Vt)-1);
Geq = Is_bjt/Vt * exp(Vd/Vt);
Ieq = Id - Geq* Vd;
tmp_s = new DCISource(non_linear_devices[i]->give_trinodeinfo().y, non_linear_devices[0]->give_trinodeinfo().z, Ieq, "NA" );
tmp_s2 = new DCISource(non_linear_devices[i]->give_trinodeinfo().x, non_linear_devices[0]->give_trinodeinfo().z, Id*beta, "NA" );
tmp_id = new Resistor(non_linear_devices[i]->give_trinodeinfo().y, non_linear_devices[0]->give_trinodeinfo().z, 1/Geq);
dc_sources.push_back(tmp_s);
dc_sources.push_back(tmp_s2);
dc_impedance_devices.push_back(tmp_id);
}
else if(non_linear_devices[i]->get_model() == "PNP"){
Id = Is_bjt/beta * (exp(Vd/Vt)-1);
Geq = Is_bjt/Vt * exp(Vd/Vt);
Ieq = Id - Geq* Vd;
tmp_s = new DCISource(non_linear_devices[i]->give_trinodeinfo().z, non_linear_devices[i]->give_trinodeinfo().y, Ieq, "NA");
tmp_s2 = new DCISource(non_linear_devices[i]->give_trinodeinfo().z, non_linear_devices[i]->give_trinodeinfo().x, Id*beta, "NA");
tmp_id = new Resistor(non_linear_devices[i]->give_trinodeinfo().z, non_linear_devices[i]->give_trinodeinfo().y, 1/Geq);
dc_sources.push_back(tmp_s);
dc_sources.push_back(tmp_s2);
dc_impedance_devices.push_back(tmp_id);
}
else if(non_linear_devices[i]->get_model() == "NMOS"){
Id = 0.5 * Kp * pow(Vd, 2);
tmp_s = new DCISource(non_linear_devices[i]->give_trinodeinfo().x, non_linear_devices[i]->give_trinodeinfo().z, Id, "NA");
dc_sources.push_back(tmp_s);
}