52 using namespace fourier;
58 nlnodes = lnnodes = banodes = nanodes = NULL;
60 NA = YV = JQ = JG = JF = NULL;
61 OM = IR = QR = RH = IG = FQ = VS = VP = FV = IL = IN = IC = IS = NULL;
71 nlnodes = lnnodes = banodes = nanodes = NULL;
73 NA = YV = JQ = JG = JF = NULL;
74 OM = IR = QR = RH = IG = FQ = VS = VP = FV = IL = IN = IC = IS = NULL;
83 if (nlnodes)
delete nlnodes;
84 if (lnnodes)
delete lnnodes;
85 if (banodes)
delete banodes;
86 if (nanodes)
delete nanodes;
117 if (ndfreqs)
delete[] ndfreqs;
123 frequency = o.frequency;
124 negfreqs = o.negfreqs;
125 posfreqs = o.posfreqs;
131 NA = YV = JQ = JG = JF = NULL;
132 OM = IR = QR = RH = IG = FQ = VS = VP = FV = IL = IN = IC = IS = NULL;
138 #define VS_(r) (*VS) (r)
139 #define OM_(r) (*OM) (r)
145 int iterations = 0, done = 0;
175 fprintf (stderr,
"IC -- constant current in f:\n"); IC->
print ();
183 fprintf (stderr,
"\n -- iteration step: %d\n", iterations);
184 fprintf (stderr,
"vs -- voltage in t:\n"); vs->
print ();
192 fprintf (stderr,
"IG -- current in t:\n"); IG->
print ();
206 fprintf (stderr,
"VS -- voltage in f:\n"); VS->
print ();
208 fprintf (stderr,
"IG -- current in f:\n"); IG->
print ();
209 fprintf (stderr,
"IR -- corrected Jacobi current in f:\n"); IR->
print ();
216 fprintf (stderr,
"FV -- error vector F(V) in f:\n"); FV->
print ();
217 fprintf (stderr,
"IL -- linear currents in f:\n"); IL->
print ();
218 fprintf (stderr,
"IN -- non-linear currents in f:\n"); IN->
print ();
219 fprintf (stderr,
"RH -- right-hand side currents in f:\n"); RH->
print ();
229 fprintf (stderr,
"JG -- G-Jacobian in t:\n"); JG->
print ();
230 fprintf (stderr,
"JQ -- C-Jacobian in t:\n"); JQ->
print ();
240 fprintf (stderr,
"JQ -- dQ/dV C-Jacobian in f:\n"); JQ->
print ();
241 fprintf (stderr,
"JG -- dI/dV G-Jacobian in f:\n"); JG->
print ();
248 fprintf (stderr,
"JF -- full Jacobian in f:\n"); JF->
print ();
255 fprintf (stderr,
"VS -- next voltage in f:\n"); VS->
print ();
262 while (!done && iterations < MaxIterations);
264 if (iterations >= MaxIterations) {
266 e->
setText (
"no convergence in %s analysis after %d iterations",
299 c->calcHB (self->frequency);
338 for (i = 0; i <= n + 1; i++) {
339 for (k = 0; k < len; k++) {
340 negfreqs.
add (i * f + nfreqs.
get (k));
343 for (i = -n; i < 0; i++) {
344 for (k = 0; k < len; k++) {
345 negfreqs.
add (i * f + nfreqs.
get (k));
348 for (i = 0; i <= 2 * n + 1; i++) {
349 for (k = 0; k < len; k++) {
350 posfreqs.
add (i * f + pfreqs.
get (k));
356 for (i = 0; i <= n + 1; i++) negfreqs.
add (i * f);
357 for (i = -n; i < 0; i++) negfreqs.
add (i * f);
358 for (i = 0; i <= 2 * n + 1; i++) posfreqs.
add (i * f);
364 int o, order = n * 2;
365 for (o = 1; o < order; o <<= 1) ;
379 if (ndfreqs)
delete[] ndfreqs;
399 if (negfreqs.
getSize () == 0) {
407 ndfreqs =
new int[dfreqs.
getSize ()];
408 for (i = 0; i < dfreqs.
getSize (); i++) {
409 ndfreqs[
i] = (n + 1) * 2;
414 for (i = 0; i < negfreqs.
getSize (); i++) {
421 for (n = 0; n < negfreqs.
getSize (); n++) {
422 if ((f = negfreqs (n)) < 0.0)
continue;
430 for (n = i = 0; n < nlfreqs; n++, i++)
438 if (
c->isNonLinear ()) {
460 if (strcmp (n,
"gnd")) {
490 nanodes =
new strlist (*nlnodes);
494 nanodes->append (*it);
497 if (!nanodes->contains (*it))
498 nanodes->append (*it);
501 banodes =
new strlist (*nlnodes);
505 fprintf (stderr,
" nanodes nodes: [ %s ]\n", nanodes->toString ());
527 for (
int nr = 0; nr < nodes->
length (); nr++) {
528 char * nn = nodes->
get (nr);
547 nnlvsrcs = excitations.
length ();
548 nnanodes = nanodes->
length ();
549 nexnodes = exnodes->
length ();
550 nbanodes = banodes->
length ();
575 (*it)->calcHB (freq);
587 #define A_(r,c) (*A) ((r)*lnfreqs+f,(c)*lnfreqs+f)
588 #define G_(r,c) A_(r,c)
589 #define B_(r,c) A_(r,c+N)
590 #define C_(r,c) A_(r+N,c)
591 #define D_(r,c) A_(r+N,c+N)
605 for (r = 0; r <
s; r++) {
607 for (c = 0; c <
s; c++) {
609 G_(nr, nc) += cir->
getY (r, c);
617 for (r = 0; r <
s; r++) {
619 for (c = 0; c < v; c++) {
621 B_(nr, nc) += cir->
getB (r, nc);
626 for (r = 0; r < v; r++) {
628 for (c = 0; c <
s; c++) {
630 C_(nr, nc) += cir->
getC (nr, c);
635 for (r = 0; r < v; r++) {
637 for (c = 0; c < v; c++) {
639 D_(nr, nc) += cir->
getD (nr, nc);
670 for (
int c = 0;
c <
N;
c++) {
675 for (
int r = 0; r <
N; r++) H->
set (r,
c, x->
get (r));
682 #define V_(r) (*V) (r)
683 #define I_(r) (*I) (r)
685 #define A_(r,c) (*A) (r,c)
687 #define Z_(r,c) (*Z) (r,c)
688 #define Y_(r,c) (*Y) (r,c)
690 #define ZVU_(r,c) Z_(r,c)
691 #define ZVL_(r,c) Z_((r)*lnfreqs+f+sn,c)
692 #define ZCU_(r,c) Z_(r,(c)*lnfreqs+f+sn)
693 #define ZCL_(r,c) Z_((r)*lnfreqs+f+sn,(c)*lnfreqs+f+sn)
695 #define YV_(r,c) (*YV) (r,c)
696 #define NA_(r,c) (*NA) (r,c)
697 #define JF_(r,c) (*JF) (r,c)
716 int sa = (N +
M) * lnfreqs;
732 int sn = sv * lnfreqs;
737 for (c = 0; c < sv * lnfreqs; c++)
A_(c, c) += 0.01;
745 for (f = 0; f < lnfreqs; f++) {
746 int pn = (pnode - 1) * lnfreqs + f;
747 int nn = (nnode - 1) * lnfreqs + f;
748 if (pnode)
A_(pn, pn) += 0.01;
749 if (nnode)
A_(nn, nn) += 0.01;
750 if (pnode && nnode) {
760 eqns.passEquationSys (A, V, I);
773 for (c = 0; c < sn; c++) {
776 eqns.passEquationSys (A, V, I);
781 for (r = 0; r < sn; r++)
ZVU_(r, c) =
V_(r);
788 for (f = 0; f < lnfreqs; f++) {
803 for (f = 0; f < lnfreqs; f++) {
804 int pn = (pnode - 1) * lnfreqs + f;
805 int nn = (nnode - 1) * lnfreqs + f;
807 if (pnode)
I_(pn) = +1.0;
808 if (nnode)
I_(nn) = -1.0;
809 eqns.passEquationSys (A, V, I);
814 for (r = 0; r < sn; r++) {
838 for (c = 0; c < sy * lnfreqs; c++)
Y_(c, c) -= 0.01;
861 if (pnode) z +=
V_((pnode - 1) * lnfreqs + f);
862 if (nnode) z -=
V_((nnode - 1) * lnfreqs + f);
870 int se = nnlvsrcs * lnfreqs;
871 int sn = nbanodes * lnfreqs;
880 for (
int f = 0; f < rfreqs.
getSize (); f++) {
881 nr_double_t freq = rfreqs (f);
892 for (r = 0; r < sn; r++) {
894 for (c = 0; c < se; c++) {
895 i +=
Y_(r, c + sn) * VC (c);
898 if (f != 0 && f != lnfreqs - 1) i /= 2;
909 for (r = 0; r < se; r++) {
911 for (c = 0; c < se; c++) {
912 i +=
Y_(r + sn, c + sn) * VC (c);
928 for (n = 0; n < len; n++) {
930 nr_double_t v_abs =
abs (VS->
get (n) - VP->
get (n));
931 nr_double_t v_rel =
abs (VS->
get (n));
932 if (v_abs >= vabstol + reltol * v_rel)
return 0;
937 nr_double_t i_abs =
abs (il + in);
938 nr_double_t i_rel =
abs ((il + in) / (il - in));
939 if (i_abs >= iabstol && 2.0 * i_rel >= reltol)
return 0;
948 #define G_(r,c) (*jg) ((r)*nlfreqs+f,(c)*nlfreqs+f)
949 #define C_(r,c) (*jq) ((r)*nlfreqs+f,(c)*nlfreqs+f)
952 #define FI_(r) (*ig) ((r)*nlfreqs+f)
953 #define FQ_(r) (*fq) ((r)*nlfreqs+f)
954 #define IR_(r) (*ir) ((r)*nlfreqs+f)
955 #define QR_(r) (*qr) ((r)*nlfreqs+f)
972 for (r = 0; r <
s; r++) {
975 for (c = 0; c <
s; c++) {
977 G_(nr, nc) += cir->
getY (r, c);
978 C_(nr, nc) += cir->
getQV (r, c);
1050 (*it)->initHB (nlfreqs);
1058 for (r = 0; r <
s; r++) {
1077 for (
int f = 0; f < nlfreqs; f++) {
1095 nr_double_t * d = (nr_double_t *) V->
getData ();
1099 for (k = i = 0; i < nodes; i++, k += 2 *
n) {
1100 nr_double_t * dst = &d[k];
1102 if (isign > 0)
for (r = 0; r < 2 *
n; r++) *dst++ /= n;
1107 for (k = i = 0; i < nodes; i++, k += 2 *
n) {
1108 nr_double_t * dst = &d[k];
1109 _fft_nd (dst, ndfreqs, nd, isign);
1110 if (isign > 0)
for (r = 0; r < 2 *
n; r++) *dst++ /= ndfreqs[0];
1135 for (
int r = 0; r < M->
getRows (); r++) {
1143 for (nc = c = 0; c < nbanodes; c++, nc += nlfreqs) {
1144 for (nr = r = 0; r < nbanodes; r++, nr += nlfreqs) {
1148 for (fc = 0; fc < nlfreqs; fc++) V (fc) = M->
get (nr + fc, nc + fc);
1151 for (fc = 0; fc < nlfreqs; fc++) {
1152 for (fi = nlfreqs - 1 - fc, fr = 0; fr < nlfreqs; fr++) {
1153 if (++fi >= nlfreqs) fi = 0;
1154 M->
set (nr + fr, nc + fc, V (fi));
1173 for (
int r = 0; r < nbanodes * nlfreqs; ) {
1175 for (
int f = 0; f < nlfreqs; f++, r++) {
1182 for (
int c = 0;
c < nbanodes * nlfreqs;
c++) {
1186 in +=
OM_(f) * FQ->
get (r);
1191 ir +=
OM_(f) * QR->
get (r);
1194 FV->
set (r, il + in);
1203 int c, r, fc, fr, rt, ct;
1206 for (c = 0; c < nbanodes; c++) {
1208 for (fc = 0; fc < nlfreqs; fc++, ct++) {
1209 for (r = 0; r < nbanodes; r++) {
1211 for (fr = 0; fr < nlfreqs; fr++, rt++) {
1212 JF_(rt, ct) = JG->
get (rt, ct) + JQ->
get (rt, ct) *
OM_(fr);
1226 for (r = 0; r < nodes; r++) {
1230 for (ff = 0; ff < lnfreqs; ff++, rf++, rt++) {
1234 for (rf -= 2; ff < nlfreqs; ff++, rf--, rt++) {
1235 res (rt) =
conj (V (rf));
1246 int r,
c, rf, rt, cf, ct, ff;
1247 for (r = 0; r < nodes; r++) {
1248 for (c = 0; c < nodes; c++) {
1254 for (ff = 0; ff < lnfreqs; ff++, cf++, ct++, rf++, rt++) {
1255 res (rt, ct) =
M (rf, cf);
1258 for (cf -= 2, rf -= 2; ff < nlfreqs; ff++, cf--, ct++, rf--, rt++) {
1259 res (rt, ct) =
conj (M (rf, cf));
1301 for (
int r = 0; r < no; r++) {
1302 for (
int c = 0;
c < no;
c++) {
1303 res (r,
c) =
M (r,
c);
1315 int of = lnfreqs * (nlnvsrcs + nnanodes);
1323 for (
int f = 0; f < lnfreqs; f++, sc++) {
1325 nr_double_t freq = rfreqs (f);
1329 int pn = (pnode - 1) * lnfreqs + f;
1330 int nn = (nnode - 1) * lnfreqs + f;
1350 int N = nnanodes * lnfreqs;
1363 for (
int n = 0;
n < nbanodes;
n++) {
1364 for (
int f = 0; f < lnfreqs; f++) {
1366 if (f != 0 && f != lnfreqs - 1) i *= 2;
1367 I_(
n * lnfreqs + f) =
i;
1394 f =
new vector (
"hbfrequency");
1399 for (
int i = 0;
i < lnfreqs;
i++) f->
add (rfreqs (
i));
1404 int l = strlen (*it);
1405 char *
n = (
char *) malloc (l + 4);
1406 sprintf (n,
"%s.Vb", *it);
1407 for (
int i = 0;
i < lnfreqs;
i++) {