forked from mbrucher/Halite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
halite.cpp
1118 lines (916 loc) · 29.3 KB
/
halite.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 <vector>
#include <string>
#include <cstdio>
#include <cmath>
namespace
{
// gMin for diodes etc..
constexpr double gMin = 1e-12;
// voltage tolerance
constexpr double vTolerance = 5e-5;
// thermal voltage for diodes/transistors
constexpr double vThermal = 0.026;
constexpr unsigned maxIter = 200;
constexpr bool VERBOSE_LU = false;
constexpr int unitValueOffset = 4;
constexpr int unitValueMax = 8;
constexpr const char* unitValueSuffixes[] = {
"p", "n", "u", "m", "", "k", "M", "G"
};
void formatUnitValue(char * buf, double v, const char * unit)
{
int suff = unitValueOffset + std::lround(std::floor(std::log(v) / std::log(10.))) / 3;
if(v < 1) suff -= 1;
if(suff < 0) suff = 0;
if(suff > unitValueMax) suff = unitValueMax;
double vr = v / std::pow(10., 3*double(suff - unitValueOffset));
sprintf(buf, "%.0f%s%s", vr, unitValueSuffixes[suff], unit);
}
}
//
// General overview
// ----------------
//
// Circuits are built from nodes and Components, where nodes are
// simply positive integers (with 0 designating ground).
//
// Every Component has one or more pins connecting to the circuit
// nodes as well as zero or more internal nets.
//
// While we map pins directly to nets here, the separation would
// be useful if the solver implemented stuff like net-reordering.
//
// MNACell represents a single entry in the solution matrix,
// where we store constants and time-step dependent constants
// separately, plus collect pointers to dynamic variables.
//
// We track enough information here that we only need to stamp once.
//
struct MNACell
{
double g; // simple values (eg. resistor conductance)
double gtimed; // time-scaled values (eg. capacitor conductance)
// pointers to dynamic variables, added in once per solve
std::vector<double*> gdyn;
double lu, prelu; // lu-solver values and matrix pre-LU cache
std::string txt; // text version of MNA for pretty-printing
void clear()
{
g = 0;
gtimed = 0;
txt = "";
}
void initLU(double stepScale)
{
prelu = g + gtimed * stepScale;
}
// restore matrix state and update dynamic values
void updatePre()
{
lu = prelu;
for(int i = 0; i < gdyn.size(); ++i)
{
lu += *(gdyn[i]);
}
}
};
// this is for keeping track of node information
// for the purposes of more intelligent plotting
struct MNANodeInfo
{
enum Type
{
tVoltage,
tCurrent,
tCount
};
Type type; // one auto-range per unit-type
double scale; // scale factor (eg. charge to voltage)
std::string name; // node name for display
};
// Stores A and b for A*x - b = 0, where x is the solution.
//
// A is stored as a vector of rows, for easy in-place pivots
//
struct MNASystem
{
typedef std::vector<MNACell> MNAVector;
typedef std::vector<MNAVector> MNAMatrix;
// node names - for output
std::vector<MNANodeInfo> nodes;
MNAMatrix A;
MNAVector b;
double time;
void setSize(int n)
{
A.resize(n);
b.resize(n);
nodes.resize(n);
for(unsigned i = 0; i < n; ++i)
{
b[i].clear();
A[i].resize(n);
char buf[16];
sprintf(buf, "v%d", i);
nodes[i].name = buf;
nodes[i].type = MNANodeInfo::tVoltage;
nodes[i].scale = 1;
for(unsigned j = 0; j < n; ++j)
{
A[i][j].clear();
}
}
time = 0;
}
void stampTimed(double g, int r, int c, const std::string & txt)
{
A[r][c].gtimed += g;
A[r][c].txt += txt;
}
void stampStatic(double g, int r, int c, const std::string & txt)
{
A[r][c].g += g;
A[r][c].txt += txt;
}
};
struct IComponent
{
virtual ~IComponent() {}
// return the number of pins for this component
virtual int pinCount() = 0;
// return a pointer to array of pin locations
// NOTE: these will eventually be GUI locations to be unified
virtual const int* getPinLocs() const = 0;
// setup pins and calculate the size of the full netlist
// the Component<> will handle this automatically
//
// - netSize is the current size of the netlist
// - pins is an array of circuits nodes
//
virtual void setupNets(int & netSize, int & states, const int* pins) = 0;
// stamp constants into the matrix
virtual void stamp(MNASystem & m) = 0;
// this is for allocating state variables
virtual void setupStates(int & states) {}
// update state variables, only tagged nodes
// this is intended for fixed-time compatible
// testing to make sure we can code-gen stuff
virtual void update(MNASystem & m) {}
// return true if we're done - will keep iterating
// until all the components are happy
virtual bool newton(MNASystem & m) { return true; }
// time-step change, for caps to fix their state-variables
virtual void scaleTime(double told_per_new) {}
};
template <int nPins = 0, int nInternalNets = 0>
struct Component : IComponent
{
static const int nNets = nPins + nInternalNets;
int pinLoc[nPins];
int nets[nNets];
int pinCount() final { return nPins; }
const int* getPinLocs() const final { return pinLoc; }
void setupNets(int & netSize, int & states, const int* pins) final
{
for(int i = 0; i < nPins; ++i)
{
nets[i] = pins[i];
}
for(int i = 0; i < nInternalNets; ++i)
{
nets[nPins + i] = netSize++;
}
setupStates(states);
}
};
struct Resistor : Component<2>
{
double r;
Resistor(double r, int l0, int l1) : r(r)
{
pinLoc[0] = l0;
pinLoc[1] = l1;
}
void stamp(MNASystem & m) final
{
char txt[16];
txt[0] = 'R';
formatUnitValue(txt+1, r, "");
double g = 1. / r;
m.stampStatic(+g, nets[0], nets[0], std::string("+") + txt);
m.stampStatic(-g, nets[0], nets[1], std::string("-") + txt);
m.stampStatic(-g, nets[1], nets[0], std::string("-") + txt);
m.stampStatic(+g, nets[1], nets[1], std::string("+") + txt);
}
};
struct Capacitor : Component<2, 1>
{
double c;
double stateVar;
double voltage;
Capacitor(double c, int l0, int l1) : c(c)
{
pinLoc[0] = l0;
pinLoc[1] = l1;
stateVar = 0;
voltage = 0;
}
void stamp(MNASystem & m) final
{
char buf[16];
formatUnitValue(buf, c, "F");
// we can use a trick here, to get the capacitor to
// work on it's own line with direct trapezoidal:
//
// | -g*t +g*t +t | v+
// | +g*t -g*t -t | v-
// | +2*g -2*g -1 | state
//
// the logic with this is that for constant timestep:
//
// i1 = g*v1 - s0 , s0 = g*v0 + i0
// s1 = 2*g*v1 - s0 <-> s0 = 2*g*v1 - s1
//
// then if we substitute back:
// i1 = g*v1 - (2*g*v1 - s1)
// = s1 - g*v1
//
// this way we just need to copy the new state to the
// next timestep and there's no actual integration needed
//
// the "half time-step" error here means that our state
// is 2*c*v - i/t but we fix this for display in update
// and correct the current-part on time-step changes
// trapezoidal needs another factor of two for the g
// since c*(v1 - v0) = (i1 + i0)/(2*t), where t = 1/T
double g = 2*c;
m.stampTimed(+1, nets[0], nets[2], "+t");
m.stampTimed(-1, nets[1], nets[2], "-t");
m.stampTimed(-g, nets[0], nets[0], std::string("-t*") + buf);
m.stampTimed(+g, nets[0], nets[1], std::string("+t*") + buf);
m.stampTimed(+g, nets[1], nets[0], std::string("+t*") + buf);
m.stampTimed(-g, nets[1], nets[1], std::string("-t*") + buf);
m.stampStatic(+2*g, nets[2], nets[0], std::string("+2*") + buf);
m.stampStatic(-2*g, nets[2], nets[1], std::string("-2*") + buf);
m.stampStatic(-1, nets[2], nets[2], "-1");
// see the comment about v:C[%d] below
sprintf(buf, "q:C:%d,%d", pinLoc[0], pinLoc[1]);
m.b[nets[2]].gdyn.push_back(&stateVar);
m.b[nets[2]].txt = buf;
// this isn't quite right as state stores 2*c*v - i/t
// however, we'll fix this in updateFull() for display
sprintf(buf, "v:C:%d,%d", pinLoc[0], pinLoc[1]);
m.nodes[nets[2]].name = buf;
m.nodes[nets[2]].scale = 1 / c;
}
void update(MNASystem & m) final
{
stateVar = m.b[nets[2]].lu;
// solve legit voltage from the pins
voltage = m.b[nets[0]].lu - m.b[nets[1]].lu;
// then we can store this for display here
// since this value won't be used at this point
m.b[nets[2]].lu = c*voltage;
}
void scaleTime(double told_per_new) final
{
// the state is 2*c*voltage - i/t0
// so we subtract out the voltage, scale current
// and then add the voltage back to get new state
//
// note that this also works if the old rate is infinite
// (ie. t0=0) when going from DC analysis to transient
//
double qq = 2*c*voltage;
stateVar = qq + (stateVar - qq)*told_per_new;
}
};
struct Voltage : Component<2, 1>
{
double v;
Voltage(double v, int l0, int l1) : v(v)
{
pinLoc[0] = l0;
pinLoc[1] = l1;
}
void stamp(MNASystem & m) final
{
m.stampStatic(-1, nets[0], nets[2], "-1");
m.stampStatic(+1, nets[1], nets[2], "+1");
m.stampStatic(+1, nets[2], nets[0], "+1");
m.stampStatic(-1, nets[2], nets[1], "-1");
char buf[16];
sprintf(buf, "%+.2gV", v);
m.b[nets[2]].g = v;
m.b[nets[2]].txt = buf;
sprintf(buf, "i:V(%+.2g):%d,%d", v, pinLoc[0], pinLoc[1]);
m.nodes[nets[2]].name = buf;
m.nodes[nets[2]].type = MNANodeInfo::tCurrent;
}
};
// probe a differential voltage
// also forces this voltage to actually get solved :)
struct Probe : Component<2, 1>
{
Probe(int l0, int l1)
{
pinLoc[0] = l0;
pinLoc[1] = l1;
}
void stamp(MNASystem & m) final
{
// vp + vn - vd = 0
m.stampStatic(+1, nets[2], nets[0], "+1");
m.stampStatic(-1, nets[2], nets[1], "-1");
m.stampStatic(-1, nets[2], nets[2], "-1");
m.nodes[nets[2]].name = "v:probe";
}
//void update(MNASystem & m)
//{
// we could do output here :)
//}
};
// function voltage generator
struct Function : Component<2,1>
{
typedef double (*FuncPtr)(double t);
FuncPtr fn;
double v;
Function(FuncPtr fn, int l0, int l1) : fn(fn)
{
pinLoc[0] = l0;
pinLoc[1] = l1;
v = fn(0);
}
void stamp(MNASystem & m) final
{
// this is identical to voltage source
// except voltage is dynanic
m.stampStatic(-1, nets[0], nets[2], "-1");
m.stampStatic(+1, nets[1], nets[2], "+1");
m.stampStatic(+1, nets[2], nets[0], "+1");
m.stampStatic(-1, nets[2], nets[1], "-1");
char buf[16];
m.b[nets[2]].gdyn.push_back(&v);
sprintf(buf, "Vfn:%d,%d", pinLoc[0], pinLoc[1]);
m.b[nets[2]].txt = buf;
sprintf(buf, "i:Vfn:%d,%d", pinLoc[0], pinLoc[1]);
m.nodes[nets[2]].name = buf;
m.nodes[nets[2]].type = MNANodeInfo::tCurrent;
}
void update(MNASystem & m) final
{
v = fn(m.time);
}
};
// POD-struct for PN-junction data, for diodes and BJTs
//
struct JunctionPN
{
// variables
double geq, ieq, veq;
// parameters
double is, nvt, rnvt, vcrit;
};
void initJunctionPN(JunctionPN & pn, double is, double n)
{
pn.is = is;
pn.nvt = n * vThermal;
pn.rnvt = 1 / pn.nvt;
pn.vcrit = pn.nvt * log(pn.nvt / (pn.is * sqrt(2.)));
}
// linearize junction at the specified voltage
//
// ideally we could handle series resistance here as well
// to avoid putting it on a separate node, but not sure how
// to make that work as it looks like we'd need Lambert-W then
void linearizeJunctionPN(JunctionPN & pn, double v)
{
double e = pn.is * exp(v * pn.rnvt);
double i = e - pn.is + gMin * v;
double g = e * pn.rnvt + gMin;
pn.geq = g;
pn.ieq = v*g - i;
pn.veq = v;
}
// returns true if junction is good enough
bool newtonJunctionPN(JunctionPN & pn, double v)
{
double dv = v - pn.veq;
if(fabs(dv) < vTolerance) return true;
// check critical voltage and adjust voltage if over
if(v > pn.vcrit)
{
// this formula comes from Qucs documentation
v = pn.veq + pn.nvt*log((std::max)(pn.is, 1+dv*pn.rnvt));
}
linearizeJunctionPN(pn, v);
return false;
}
struct Diode : Component<2, 2>
{
JunctionPN pn;
// should make these parameters
double rs;
// l0 -->|-- l1 -- parameters default to approx 1N4148
Diode(int l0, int l1,
double rs = 10., double is = 35e-12, double n = 1.24)
: rs(rs)
{
pinLoc[0] = l0;
pinLoc[1] = l1;
initJunctionPN(pn, is, n);
// FIXME: move init to some restart routine?
// initial condition v = 0
linearizeJunctionPN(pn, 0);
}
bool newton(MNASystem & m) final
{
return newtonJunctionPN(pn, m.b[nets[2]].lu);
}
void stamp(MNASystem & m) final
{
// Diode could be built with 3 extra nodes:
//
// | . . . . +1 | V+
// | . . . . -1 | V-
// | . . grs -grs -1 | v:D
// | . . -grs grs+geq . | v:pn = ieq
// | -1 +1 +1 . . | i:pn
//
// Here grs is the 1/rs series conductance.
//
// This gives us the junction voltage (v:pn) and
// current (i:pn) and the composite voltage (v:D).
//
// The i:pn row is an ideal transformer connecting
// the floating diode to the ground-referenced v:D
// where we connect the series resistance to v:pn
// that solves the diode equation with Newton.
//
// We can then add the 3rd row to the bottom 2 with
// multipliers 1 and -rs = -1/grs and drop it:
//
// | . . . +1 | V+
// | . . . -1 | V-
// | . . geq -1 | v:pn = ieq
// | -1 +1 +1 rs | i:pn
//
// Note that only the v:pn row here is non-linear.
//
// We could even do away without the separate row for
// the current, which would lead to the following:
//
// | +grs -grs -grs |
// | -grs +grs +grs |
// | -grs +grs +grs+geq | = ieq
//
// In practice we keep the current row since it's
// nice to have it as an output anyway.
//
m.stampStatic(-1, nets[3], nets[0], "-1");
m.stampStatic(+1, nets[3], nets[1], "+1");
m.stampStatic(+1, nets[3], nets[2], "+1");
m.stampStatic(+1, nets[0], nets[3], "+1");
m.stampStatic(-1, nets[1], nets[3], "-1");
m.stampStatic(-1, nets[2], nets[3], "-1");
m.stampStatic(rs, nets[3], nets[3], "rs:pn");
m.A[nets[2]][nets[2]].gdyn.push_back(&pn.geq);
m.A[nets[2]][nets[2]].txt = "gm:D";
m.b[nets[2]].gdyn.push_back(&pn.ieq);
char buf[16];
sprintf(buf, "i0:D:%d,%d", pinLoc[0], pinLoc[1]);
m.b[nets[2]].txt = buf;
sprintf(buf, "v:D:%d,%d", pinLoc[0], pinLoc[1]);
m.nodes[nets[2]].name = buf;
sprintf(buf, "i:D:%d,%d", pinLoc[0], pinLoc[1]);
m.nodes[nets[3]].name = buf;
m.nodes[nets[3]].type = MNANodeInfo::tCurrent;
}
};
struct BJT : Component<3, 4>
{
// emitter and collector junctions
JunctionPN pnC, pnE;
// forward and reverse alpha
double af, ar, rsbc, rsbe;
bool pnp;
BJT(int b, int c, int e, bool pnp = false) : pnp(pnp)
{
pinLoc[0] = b;
pinLoc[1] = c;
pinLoc[2] = e;
// this attempts a 2n3904-style small-signal
// transistor, although the values are a bit
// arbitrarily set to "something reasonable"
// forward and reverse beta
double bf = 200;
double br = 20;
// forward and reverse alpha
af = bf / (1 + bf);
ar = br / (1 + br);
// these are just rb+re and rb+rc
// this is not necessarily the best way to
// do anything, but having junction series
// resistances helps handle degenerate cases
rsbc = 5.8376+0.0001;
rsbe = 5.8376+2.65711;
//
// the basic rule is that:
// af * ise = ar * isc = is
//
// FIXME: with non-equal ideality factors
// we can get non-sensical results, why?
//
double is = 6.734e-15;
double n = 1.24;
initJunctionPN(pnE, is / af, n);
initJunctionPN(pnC, is / ar, n);
linearizeJunctionPN(pnE, 0);
linearizeJunctionPN(pnC, 0);
}
bool newton(MNASystem & m) final
{
return newtonJunctionPN(pnC, m.b[nets[3]].lu)
& newtonJunctionPN(pnE, m.b[nets[4]].lu);
}
void stamp(MNASystem & m) final
{
// The basic idea here is the same as with diodes
// except we do it once for each junction.
//
// With the transfer currents sourced from the
// diode currents, NPN then looks like this:
//
// 0 | . . . . . 1-ar 1-af | vB
// 1 | . . . . . -1 +af | vC
// 2 | . . . . . +ar -1 | vE
// 3 | . . . gc . -1 . | v:Qbc = ic
// 4 | . . . . ge . -1 | v:Qbe = ie
// 5 | -1 +1 . +1 . rsbc . | i:Qbc
// 6 | -1 . +1 . +1 . rsbe | i:Qbe
// ------------------------
// 0 1 2 3 4 5 6
//
// For PNP version, we simply flip the junctions
// by changing signs of (3,5),(5,3) and (4,6),(6,4).
//
// Also just like diodes, we have junction series
// resistances, rather than terminal resistances.
//
// This works just as well, but should be kept
// in mind when fitting particular transistors.
//
// diode currents to external base
m.stampStatic(1-ar, nets[0], nets[5], "1-ar");
m.stampStatic(1-af, nets[0], nets[6], "1-af");
// diode currents to external collector and emitter
m.stampStatic(-1, nets[1], nets[5], "-1");
m.stampStatic(-1, nets[2], nets[6], "-1");
// series resistances
m.stampStatic(rsbc, nets[5], nets[5], "rsbc");
m.stampStatic(rsbe, nets[6], nets[6], "rsbe");
// current - junction connections
// for the PNP case we flip the signs of these
// to flip the diode junctions wrt. the above
if(pnp)
{
m.stampStatic(-1, nets[5], nets[3], "-1");
m.stampStatic(+1, nets[3], nets[5], "+1");
m.stampStatic(-1, nets[6], nets[4], "-1");
m.stampStatic(+1, nets[4], nets[6], "+1");
}
else
{
m.stampStatic(+1, nets[5], nets[3], "+1");
m.stampStatic(-1, nets[3], nets[5], "-1");
m.stampStatic(+1, nets[6], nets[4], "+1");
m.stampStatic(-1, nets[4], nets[6], "-1");
}
// external voltages to collector current
m.stampStatic(-1, nets[5], nets[0], "-1");
m.stampStatic(+1, nets[5], nets[1], "+1");
// external voltages to emitter current
m.stampStatic(-1, nets[6], nets[0], "-1");
m.stampStatic(+1, nets[6], nets[2], "+1");
// source transfer currents to external pins
m.stampStatic(+ar, nets[2], nets[5], "+ar");
m.stampStatic(+af, nets[1], nets[6], "+af");
char buf[16];
// dynamic variables
m.A[nets[3]][nets[3]].gdyn.push_back(&pnC.geq);
m.A[nets[3]][nets[3]].txt = "gm:Qbc";
m.b[nets[3]].gdyn.push_back(&pnC.ieq);
sprintf(buf, "i0:Q:%d,%d,%d:cb", pinLoc[0], pinLoc[1], pinLoc[2]);
m.b[nets[3]].txt = buf;
m.A[nets[4]][nets[4]].gdyn.push_back(&pnE.geq);
m.A[nets[4]][nets[4]].txt = "gm:Qbe";
m.b[nets[4]].gdyn.push_back(&pnE.ieq);
sprintf(buf, "i0:Q:%d,%d,%d:eb", pinLoc[0], pinLoc[1], pinLoc[2]);
m.b[nets[4]].txt = buf;
sprintf(buf, "v:Q:%d,%d,%d:%s",
pinLoc[0], pinLoc[1], pinLoc[2], pnp ? "cb" : "bc");
m.nodes[nets[3]].name = buf;
sprintf(buf, "v:Q:%d,%d,%d:%s",
pinLoc[0], pinLoc[1], pinLoc[2], pnp ? "eb" : "be");
m.nodes[nets[4]].name = buf;
sprintf(buf, "i:Q:%d,%d,%d:bc", pinLoc[0], pinLoc[1], pinLoc[2]);
m.nodes[nets[5]].name = buf;
m.nodes[nets[5]].type = MNANodeInfo::tCurrent;
m.nodes[nets[5]].scale = 1 - ar;
sprintf(buf, "i:Q:%d,%d,%d:be", pinLoc[0], pinLoc[1], pinLoc[2]);
m.nodes[nets[6]].name = buf;
m.nodes[nets[6]].type = MNANodeInfo::tCurrent;
m.nodes[nets[6]].scale = 1 - af;
}
};
struct NetList
{
typedef std::vector<IComponent*> ComponentList;
NetList(int nodes) : nets(nodes), states(0)
{
}
void addComponent(IComponent * c)
{
// this is a bit "temporary" for now
c->setupNets(nets, states, c->getPinLocs());
components.push_back(c);
}
void buildSystem()
{
system.setSize(nets);
for(int i = 0; i < components.size(); ++i)
{
components[i]->stamp(system);
}
printf("Prepare for DC analysis..\n");
setStepScale(0);
tStep = 0;
}
void dumpMatrix()
{
std::vector<int> maxWidth(nets);
for(int i = 0; i < nets; ++i) maxWidth[i] = 1;
int nnMax = 1;
for(int i = 0; i < nets; ++i)
{
nnMax = std::max(nnMax, (int)system.nodes[i].name.size());
for(int j = 0; j < nets; ++j)
{
maxWidth[j] = std::max(maxWidth[j],
(int)system.A[i][j].txt.size());
}
}
char buf[1024];
for(unsigned i = 0; i < nets; ++i)
{
int off = sprintf(buf, "%2d: | ", i);
for(int j = 0; j < nets; ++j)
{
off += sprintf(buf+off,
" %*s ", maxWidth[j],
system.A[i][j].txt.size()
? system.A[i][j].txt.c_str()
: ((system.A[i][j].lu==0) ? "." : "#"));
}
sprintf(buf+off, " | %-*s = %s",
nnMax, system.nodes[i].name.c_str(),
system.b[i].txt.size()
? system.b[i].txt.c_str() : (!i ? "ground" : "0"));
puts(buf);
}
}
void setTimeStep(double tStepSize)
{
for(int i = 0; i < components.size(); ++i)
{
components[i]->scaleTime(tStep / tStepSize);
}
tStep = tStepSize;
double stepScale = 1. / tStep;
printf("timeStep changed to %.2g (%.2g Hz)\n", tStep, stepScale);
setStepScale(stepScale);
}
void simulateTick()
{
int iter;
for(iter = 0; iter < maxIter; ++iter)
{
// restore matrix state and add dynamic values
updatePre();
luFactor();
luForward();
luSolve();
if(newton()) break;
}
system.time += tStep;
update();
printf(" %02.4f |", system.time);
int fillPost = 0;
for(int i = 1; i < nets; ++i)
{
printf("\t%+.4e", system.b[i].lu * system.nodes[i].scale);
for(int j = 1; j < nets; ++j)
{
if(system.A[i][j].lu != 0) ++fillPost;
}
}
printf("\t %d iters, LU density: %.1f%%\n",
iter, 100 * fillPost / ((nets-1.f)*(nets-1.f)));
}
void printHeaders()
{
printf("\n time: | ");
for(int i = 1; i < nets; ++i)
{
printf("%16s", system.nodes[i].name.c_str());
}
printf("\n\n");
}
// plotting and such would want to use this
const MNASystem & getMNA() { return system; }
protected:
double tStep;
int nets, states;
ComponentList components;
MNASystem system;
void update()
{
for(int i = 0; i < components.size(); ++i)
{
components[i]->update(system);
}
}
// return true if we're done
bool newton()
{
bool done = 1;
for(int i = 0; i < components.size(); ++i)
{
done &= components[i]->newton(system);
}
return done;
}
void initLU(double stepScale)
{
for(int i = 0; i < nets; ++i)
{
system.b[i].initLU(stepScale);
for(int j = 0; j < nets; ++j)
{
system.A[i][j].initLU(stepScale);
}
}
}
void setStepScale(double stepScale)
{
// initialize matrix for LU and save it to cache
initLU(stepScale);
int fill = 0;
for(int i = 1; i < nets; ++i)
{
for(int j = 1; j < nets; ++j)
{
if(system.A[i][j].prelu != 0
|| system.A[i][j].gdyn.size()) ++fill;
}
}
printf("MNA density %.1f%%\n", 100 * fill / ((nets-1.)*(nets-1.)));
}
void updatePre()
{
for(int i = 0; i < nets; ++i)
{
system.b[i].updatePre();
for(int j = 0; j < nets; ++j)
{
system.A[i][j].updatePre();
}
}
}
void luFactor()
{
int p;
for(p = 1; p < nets; ++p)
{
// FIND PIVOT
{
int pr = p;
for(int r = p; r < nets; ++r)
{
if(fabs(system.A[r][p].lu)
> fabs(system.A[pr][p].lu))
{
pr = r;
}
}
// swap if necessary
if(pr != p)
{
std::swap(system.A[p], system.A[pr]);
std::swap(system.b[p], system.b[pr]);
}
if(VERBOSE_LU)
{
printf("pivot %d (from %d): %+.2e\n",
p, pr, system.A[p][p].lu);
}
}
if(0 == system.A[p][p].lu)
{
printf("Failed to find a pivot!!");
return;
}
// take reciprocal for D entry