My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pad3bit.core.cpp
Go to the documentation of this file.
1 /*
2  * pad3bit.core.cpp - device implementations for pad3bit module
3  *
4  * This is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  */
10 
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 
15 #include "pad3bit.analogfunction.h"
16 #include "component.h"
17 #include "device.h"
18 #include "pad3bit.core.h"
19 
20 #ifndef CIR_pad3bit
21 #define CIR_pad3bit -1
22 #endif
23 
24 // external nodes
25 #define C 0
26 #define B 1
27 #define A 2
28 // internal nodes
29 
30 // useful macro definitions
31 #define NP(node) real (getV (node))
32 #define BP(pnode,nnode) (NP(pnode) - NP(nnode))
33 #define _load_static_residual2(pnode,nnode,current)\
34  _rhs[pnode] -= current;\
35  _rhs[nnode] += current;
36 #define _load_static_augmented_residual2(pnode,nnode,current)\
37  _rhs[pnode] -= current;\
38  _rhs[nnode] += current;
39 #define _load_static_residual1(node,current)\
40  _rhs[node] -= current;
41 #define _load_static_augmented_residual1(node,current)\
42  _rhs[node] -= current;
43 #define _load_static_jacobian4(pnode,nnode,vpnode,vnnode,conductance)\
44  _jstat[pnode][vpnode] += conductance;\
45  _jstat[nnode][vnnode] += conductance;\
46  _jstat[pnode][vnnode] -= conductance;\
47  _jstat[nnode][vpnode] -= conductance;\
48  if (doHB) {\
49  _ghs[pnode] += conductance * BP(vpnode,vnnode);\
50  _ghs[nnode] -= conductance * BP(vpnode,vnnode);\
51  } else {\
52  _rhs[pnode] += conductance * BP(vpnode,vnnode);\
53  _rhs[nnode] -= conductance * BP(vpnode,vnnode);\
54  }
55 #define _load_static_jacobian2p(node,vpnode,vnnode,conductance)\
56  _jstat[node][vpnode] += conductance;\
57  _jstat[node][vnnode] -= conductance;\
58  if (doHB) {\
59  _ghs[node] += conductance * BP(vpnode,vnnode);\
60  } else {\
61  _rhs[node] += conductance * BP(vpnode,vnnode);\
62  }
63 #define _load_static_jacobian2s(pnode,nnode,node,conductance)\
64  _jstat[pnode][node] += conductance;\
65  _jstat[nnode][node] -= conductance;\
66  if (doHB) {\
67  _ghs[pnode] += conductance * NP(node);\
68  _ghs[nnode] -= conductance * NP(node);\
69  } else {\
70  _rhs[pnode] += conductance * NP(node);\
71  _rhs[nnode] -= conductance * NP(node);\
72  }
73 #define _load_static_jacobian1(node,vnode,conductance)\
74  _jstat[node][vnode] += conductance;\
75  if (doHB) {\
76  _ghs[node] += conductance * NP(vnode);\
77  } else {\
78  _rhs[node] += conductance * NP(vnode);\
79  }
80 #define _load_dynamic_residual2(pnode,nnode,charge)\
81  if (doTR) _charges[pnode][nnode] += charge;\
82  if (doHB) {\
83  _qhs[pnode] -= charge;\
84  _qhs[nnode] += charge;\
85  }
86 #define _load_dynamic_residual1(node,charge)\
87  if (doTR) _charges[node][node] += charge;\
88  if (doHB) {\
89  _qhs[node] -= charge;\
90  }
91 #define _load_dynamic_jacobian4(pnode,nnode,vpnode,vnnode,capacitance)\
92  if (doAC) {\
93  _jdyna[pnode][vpnode] += capacitance;\
94  _jdyna[nnode][vnnode] += capacitance;\
95  _jdyna[pnode][vnnode] -= capacitance;\
96  _jdyna[nnode][vpnode] -= capacitance;\
97  }\
98  if (doTR) {\
99  _caps[pnode][nnode][vpnode][vnnode] += capacitance;\
100  }\
101  if (doHB) {\
102  _chs[pnode] += capacitance * BP(vpnode,vnnode);\
103  _chs[nnode] -= capacitance * BP(vpnode,vnnode);\
104  }
105 #define _load_dynamic_jacobian2s(pnode,nnode,vnode,capacitance)\
106  if (doAC) {\
107  _jdyna[pnode][vnode] += capacitance;\
108  _jdyna[nnode][vnode] -= capacitance;\
109  }\
110  if (doTR) {\
111  _caps[pnode][nnode][vnode][vnode] += capacitance;\
112  }\
113  if (doHB) {\
114  _chs[pnode] += capacitance * NP(vnode);\
115  _chs[nnode] -= capacitance * NP(vnode);\
116  }
117 #define _load_dynamic_jacobian2p(node,vpnode,vnnode,capacitance)\
118  if (doAC) {\
119  _jdyna[node][vpnode] += capacitance;\
120  _jdyna[node][vnnode] -= capacitance;\
121  }\
122  if (doTR) {\
123  _caps[node][node][vpnode][vnnode] += capacitance;\
124  }\
125  if (doHB) {\
126  _chs[node] += capacitance * BP(vpnode,vnnode);\
127  }
128 #define _load_dynamic_jacobian1(node,vnode,capacitance)\
129  if (doAC) {\
130  _jdyna[node][vnode] += capacitance;\
131  }\
132  if (doTR) {\
133  _caps[node][node][vnode][vnode] += capacitance;\
134  }\
135  if (doHB) {\
136  _chs[node] += capacitance * NP(vnode);\
137  }
138 
139 #define _save_whitenoise1(n1,pwr,type)\
140  _white_pwr[n1][n1] += pwr;
141 #define _save_whitenoise2(n1,n2,pwr,type)\
142  _white_pwr[n1][n2] += pwr;
143 #define _save_flickernoise1(n1,pwr,exp,type)\
144  _flicker_pwr[n1][n1] += pwr;\
145  _flicker_exp[n1][n1] += exp;
146 #define _save_flickernoise2(n1,n2,pwr,exp,type)\
147  _flicker_pwr[n1][n2] += pwr;\
148  _flicker_exp[n1][n2] += exp;
149 #define _load_whitenoise2(n1,n2,pwr)\
150  cy (n1,n2) -= pwr/kB/T0; cy (n2,n1) -= pwr/kB/T0;\
151  cy (n1,n1) += pwr/kB/T0; cy (n2,n2) += pwr/kB/T0;
152 #define _load_whitenoise1(n1,pwr)\
153  cy (n1,n1) += pwr/kB/T0;
154 #define _load_flickernoise2(n1,n2,pwr,exp)\
155  cy (n1,n2) -= pwr*pow(_freq,-exp)/kB/T0;\
156  cy (n2,n1) -= pwr*pow(_freq,-exp)/kB/T0;\
157  cy (n1,n1) += pwr*pow(_freq,-exp)/kB/T0;\
158  cy (n2,n2) += pwr*pow(_freq,-exp)/kB/T0;
159 #define _load_flickernoise1(n1,pwr,exp)\
160  cy (n1,n1) += pwr*pow(_freq,-exp)/kB/T0;
161 
162 // derivative helper macros
163 #define m00_hypot(v00,x,y) v00 = xhypot(x,y);
164 #define m10_hypot(v10,v00,x,y) v10 = (x)/(v00);
165 #define m11_hypot(v11,v00,x,y) v11 = (y)/(v00);
166 #define m00_max(v00,x,y) v00 = ((x)>(y))?(x):(y);
167 #define m10_max(v10,v00,x,y) v10 = ((x)>(y))?1.0:0.0;
168 #define m11_max(v11,v00,x,y) v11 = ((x)>(y))?0.0:1.0;
169 #define m00_min(v00,x,y) v00 = ((x)<(y))?(x):(y);
170 #define m10_min(v10,v00,x,y) v10 = ((x)<(y))?1.0:0.0;
171 #define m11_min(v11,v00,x,y) v11 = ((x)<(y))?0.0:1.0;
172 #define m00_pow(v00,x,y) v00 = pow(x,y);
173 #define m10_pow(v10,v00,x,y) v10 = (x==0.0)?0.0:(v00)*(y)/(x);
174 #define m11_pow(v11,v00,x,y) v11 = (x==0.0)?0.0:(log(x)*(v00));
175 
176 #define m00_div(v00,v10,x,y) double v10=1/(y); double v00=(x)*v10;
177 #define m10_div(v10,v00,vv,x,y)
178 #define m11_div(v11,v00,vv,x,y) double v11 = -v00*vv;
179 
180 #define m00_mult(v00,v10,v11,x,y) double v10=(x); double v11=(y); double v00=v10*v11;
181 #define m00_add(v00,x,y) double v00=(x)+(y);
182 
183 #define m00_cos(v00,x) v00 = cos(x);
184 #define m10_cos(v10,v00,x) v10 = (-sin(x));
185 #define m00_sin(v00,x) v00 = sin(x);
186 #define m10_sin(v10,v00,x) v10 = (cos(x));
187 #define m00_tan(v00,x) v00 = tan(x);
188 #define m10_tan(v10,v00,x) v10 = (1.0/cos(x)/cos(x));
189 #define m00_cosh(v00,x) v00 = cosh(x);
190 #define m10_cosh(v10,v00,x) v10 = (sinh(x));
191 #define m00_sinh(v00,x) v00 = sinh(x);
192 #define m10_sinh(v10,v00,x) v10 = (cosh(x));
193 #define m00_tanh(v00,x) v00 = tanh(x);
194 #define m10_tanh(v10,v00,x) v10 = (1.0/cosh(x)/cosh(x));
195 #define m00_acos(v00,x) v00 = acos(x);
196 #define m10_acos(v10,v00,x) v10 = (-1.0/sqrt(1-x*x));
197 #define m00_asin(v00,x) v00 = asin(x);
198 #define m10_asin(v10,v00,x) v10 = (+1.0/sqrt(1-x*x));
199 #define m00_atan(v00,x) v00 = atan(x);
200 #define m10_atan(v10,v00,x) v10 = (+1.0/(1+x*x));
201 #define m00_atanh(v00,x) v00 = atanh(x);
202 #define m10_atanh(v10,v00,x) v10 = (+1.0/(1-x*x));
203 #define m00_logE(v00,x) v00 = log(x);
204 #define m10_logE(v10,v00,x) v10 = (1.0/x);
205 #define m00_log10(v00,x) v00 = log10(x);
206 #define m10_log10(v10,v00,x) v10 = (1.0/x/M_LN10);
207 #define m00_sqrt(v00,x) v00 = sqrt(x);
208 #define m10_sqrt(v10,v00,x) v10 = (0.5/v00);
209 #define m00_fabs(v00,x) v00 = fabs(x);
210 #define m10_fabs(v10,v00,x) v10 = (((x)>=0)?(+1.0):(-1.0));
211 
212 #define m00_exp(v00,x) v00 = exp(x);
213 #define m10_exp(v10,v00,x) v10 = v00;
214 
215 #define m00_abs(v00) ((v00)<(0)?(-(v00)):(v00))
216 #define m00_floor(v00,x) v00 = floor(x);
217 #define m00_limexp(v00,x) v00 = ((x)<80.0?exp(x):exp(80.0)*(x-79.0));
218 #define m10_limexp(v10,v00,x) v10 = ((x)<80.0?(v00):exp(80.0));
219 
220 #define m20_logE(v00) (-1.0/v00/v00)
221 #define m20_exp(v00) exp(v00)
222 #define m20_limexp(v00) ((v00)<80.0?exp(v00):0.0)
223 #define m20_sqrt(v00) (-0.25/(v00)/sqrt(v00))
224 #define m20_fabs(v00) 0.0
225 #define m20_pow(x,y) ((y)*((y)-1.0)*pow(x,y)/(x)/(x))
226 #define m00_vt(x) (kBoverQ*(x))
227 #define m10_vt(x) (kBoverQ)
228 
229 // simulator specific definitions
230 #define _modelname "pad3bit"
231 #define _instancename getName()
232 #define _circuit_temp (getPropertyDouble("Temp")+273.15)
233 #define _param_given(p) (isPropertyGiven(p)?1:0)
234 
235 
236 // $vt and $vt() functions
237 #define _vt_nom (kBoverQ*_circuit_temp)
238 
239 using namespace device;
240 
241 /* Device constructor. */
243 {
244  type = CIR_pad3bit;
245 }
246 
247 /* Initialization of model. */
248 void pad3bit::initModel (void)
249 {
250  // create internal nodes
251 
252  // get device model parameters
253  loadVariables ();
254  // evaluate global model equations
255  initializeModel ();
256  // evaluate initial step equations
257  initialStep ();
258  // evaluate global instance equations
259  initializeInstance ();
260 }
261 
262 /* Initialization of DC analysis. */
263 void pad3bit::initDC (void)
264 {
265  allocMatrixMNA ();
266  initModel ();
267  pol = 1;
268  restartDC ();
269  doAC = 1;
270  doTR = 0;
271  doHB = 0;
272 }
273 
274 /* Run when DC is restarted (fallback algorithms). */
276 {
277 }
278 
279 /* Initialize Verilog-AMS code. */
280 void pad3bit::initVerilog (void)
281 {
282  // initialization of noise variables
283 
284  int i1, i2, i3, i4;
285 
286  // zero charges
287  for (i1 = 0; i1 < 3; i1++) {
288  for (i2 = 0; i2 < 3; i2++) {
289  _charges[i1][i2] = 0.0;
290  } }
291 
292  // zero capacitances
293  for (i1 = 0; i1 < 3; i1++) {
294  for (i2 = 0; i2 < 3; i2++) {
295  for (i3 = 0; i3 < 3; i3++) {
296  for (i4 = 0; i4 < 3; i4++) {
297  _caps[i1][i2][i3][i4] = 0.0;
298  } } } }
299 
300  // zero right hand side, static and dynamic jacobian
301  for (i1 = 0; i1 < 3; i1++) {
302  _rhs[i1] = 0.0;
303  _qhs[i1] = 0.0;
304  _chs[i1] = 0.0;
305  _ghs[i1] = 0.0;
306  for (i2 = 0; i2 < 3; i2++) {
307  _jstat[i1][i2] = 0.0;
308  _jdyna[i1][i2] = 0.0;
309  }
310  }
311 }
312 
313 /* Load device model input parameters. */
314 void pad3bit::loadVariables (void)
315 {
316  Number = getPropertyInteger ("Number");
317 }
318 
319 /* #define's for translated code */
320 #undef _DDT
321 #define _DDT(q) q
322 #define _DYNAMIC
323 #define _DERIVATE
324 #define _DDX
325 #define _DERIVATEFORDDX
326 
327 /* Evaluate Verilog-AMS equations in model initialization. */
328 void pad3bit::initializeModel (void)
329 {
330 }
331 
332 /* Evaluate Verilog-AMS equations in instance initialization. */
333 void pad3bit::initializeInstance (void)
334 {
335 }
336 
337 /* Evaluate Verilog-AMS equations in initial step. */
338 void pad3bit::initialStep (void)
339 {
340 }
341 
342 /* Evaluate Verilog-AMS equations in final step. */
343 void pad3bit::finalStep (void)
344 {
345 }
346 
347 /* Evaluate Verilog-AMS equations in analog block. */
348 void pad3bit::calcVerilog (void)
349 {
350 
351 /* ----------------- evaluate verilog analog equations -------------------- */
352 double IC;
353 double IB;
354 double IA;
355 if
356 ((Number)==(0))
357 {
358 IA=0;
359 IB=0;
360 IC=0;
361 }
362 else
363 if
364 ((Number)==(1))
365 {
366 IA=1;
367 IB=0;
368 IC=0;
369 }
370 else
371 if
372 ((Number)==(2))
373 {
374 IA=0;
375 IB=1;
376 IC=0;
377 }
378 else
379 if
380 ((Number)==(3))
381 {
382 IA=1;
383 IB=1;
384 IC=0;
385 }
386 else
387 if
388 ((Number)==(4))
389 {
390 IA=0;
391 IB=0;
392 IC=1;
393 }
394 else
395 if
396 ((Number)==(5))
397 {
398 IA=1;
399 IB=0;
400 IC=1;
401 }
402 else
403 if
404 ((Number)==(6))
405 {
406 IA=0;
407 IB=1;
408 IC=1;
409 }
410 else
411 if
412 ((Number)==(7))
413 {
414 IA=1;
415 IB=1;
416 IC=1;
417 }
418 else
419 { /* no default */ }
421 #if defined(_DERIVATE)
422 #endif
424 #if defined(_DERIVATE)
426 #endif
428 #if defined(_DERIVATE)
429 #endif
431 #if defined(_DERIVATE)
433 #endif
435 #if defined(_DERIVATE)
436 #endif
438 #if defined(_DERIVATE)
440 #endif
441 
442 /* ------------------ end of verilog analog equations --------------------- */
443 
444 /* ------------------ evaluate verilog noise equations -------------------- */
445 
446 /* ------------------- end of verilog noise equations --------------------- */
447 }
448 
449 /* Perform DC iteration. */
450 void pad3bit::calcDC (void)
451 {
452  // evaluate Verilog code
453  initVerilog ();
454  calcVerilog ();
455 
456  // fill right hand side and static jacobian
457  for (int i1 = 0; i1 < 3; i1++) {
458  setI (i1, _rhs[i1]);
459  for (int i2 = 0; i2 < 3; i2++) {
460  setY (i1, i2, _jstat[i1][i2]);
461  }
462  }
463 }
464 
465 /* Save operating points. */
467 {
468  // save global instance operating points
469 }
470 
471 /* Load operating points. */
473 {
474 }
475 
476 /* Calculate operating points. */
478 {
479 }
480 
481 /* Initialization of AC analysis. */
482 void pad3bit::initAC (void)
483 {
484  allocMatrixMNA ();
485 }
486 
487 /* Perform AC calculations. */
488 void pad3bit::calcAC (nr_double_t frequency)
489 {
490  setMatrixY (calcMatrixY (frequency));
491 }
492 
493 /* Compute Y-matrix for AC analysis. */
494 matrix pad3bit::calcMatrixY (nr_double_t frequency)
495 {
496  _freq = frequency;
498  matrix y (3);
499 
500  for (int i1 = 0; i1 < 3; i1++) {
501  for (int i2 = 0; i2 < 3; i2++) {
502  y (i1,i2) = rect (_jstat[i1][i2], _jdyna[i1][i2] * 2 * M_PI * _freq);
503  }
504  }
505 
506  return y;
507 }
508 
509 /* Initialization of S-parameter analysis. */
510 void pad3bit::initSP (void)
511 {
512  allocMatrixS ();
513 }
514 
515 /* Perform S-parameter calculations. */
516 void pad3bit::calcSP (nr_double_t frequency)
517 {
518  setMatrixS (ytos (calcMatrixY (frequency)));
519 }
520 
521 /* Initialization of transient analysis. */
522 void pad3bit::initTR (void)
523 {
524  setStates (2 * 3 * 3);
525  initDC ();
526 }
527 
528 /* Perform transient analysis iteration step. */
529 void pad3bit::calcTR (nr_double_t)
530 {
531  doHB = 0;
532  doAC = 1;
533  doTR = 1;
534  calcDC ();
535 
536  int i1, i2, i3, i4, state;
537 
538  // 2-node charge integrations
539  for (i1 = 0; i1 < 3; i1++) {
540  for (i2 = 0; i2 < 3; i2++) {
541  state = 2 * (i2 + 3 * i1);
542  if (i1 != i2)
543  if (_charges[i1][i2] != 0.0)
544  transientCapacitanceQ (state, i1, i2, _charges[i1][i2]);
545  } }
546 
547  // 1-node charge integrations
548  for (i1 = 0; i1 < 3; i1++) {
549  state = 2 * (i1 + 3 * i1);
550  if (_charges[i1][i1] != 0.0)
551  transientCapacitanceQ (state, i1, _charges[i1][i1]);
552  }
553 
554  // charge: 2-node, voltage: 2-node
555  for (i1 = 0; i1 < 3; i1++) {
556  for (i2 = 0; i2 < 3; i2++) {
557  if (i1 != i2)
558  for (i3 = 0; i3 < 3; i3++) {
559  for (i4 = 0; i4 < 3; i4++) {
560  if (i3 != i4)
561  if (_caps[i1][i2][i3][i4] != 0.0)
562  transientCapacitanceC (i1, i2, i3, i4, _caps[i1][i2][i3][i4], BP(i3,i4));
563  } } } }
564 
565  // charge: 2-node, voltage: 1-node
566  for (i1 = 0; i1 < 3; i1++) {
567  for (i2 = 0; i2 < 3; i2++) {
568  if (i1 != i2)
569  for (i3 = 0; i3 < 3; i3++) {
570  if (_caps[i1][i2][i3][i3] != 0.0)
571  transientCapacitanceC2Q (i1, i2, i3, _caps[i1][i2][i3][i3], NP(i3));
572  } } }
573 
574  // charge: 1-node, voltage: 2-node
575  for (i1 = 0; i1 < 3; i1++) {
576  for (i3 = 0; i3 < 3; i3++) {
577  for (i4 = 0; i4 < 3; i4++) {
578  if (i3 != i4)
579  if (_caps[i1][i1][i3][i4] != 0.0)
580  transientCapacitanceC2V (i1, i3, i4, _caps[i1][i1][i3][i4], BP(i3,i4));
581  } } }
582 
583  // charge: 1-node, voltage: 1-node
584  for (i1 = 0; i1 < 3; i1++) {
585  for (i3 = 0; i3 < 3; i3++) {
586  if (_caps[i1][i1][i3][i3] != 0.0)
587  transientCapacitanceC (i1, i3, _caps[i1][i1][i3][i3], NP(i3));
588  } }
589 }
590 
591 /* Compute Cy-matrix for AC noise analysis. */
592 matrix pad3bit::calcMatrixCy (nr_double_t frequency)
593 {
594  _freq = frequency;
595  matrix cy (3);
596 
597 
598  return cy;
599 }
600 
601 /* Perform AC noise computations. */
602 void pad3bit::calcNoiseAC (nr_double_t frequency)
603 {
604  setMatrixN (calcMatrixCy (frequency));
605 }
606 
607 /* Perform S-parameter noise computations. */
608 void pad3bit::calcNoiseSP (nr_double_t frequency)
609 {
610  setMatrixN (cytocs (calcMatrixCy (frequency) * z0, getMatrixS ()));
611 }
612 
613 /* Initialization of HB analysis. */
614 void pad3bit::initHB (int)
615 {
616  initDC ();
617  allocMatrixHB ();
618 }
619 
620 /* Perform HB analysis. */
621 void pad3bit::calcHB (int)
622 {
623  doHB = 1;
624  doAC = 1;
625  doTR = 0;
626 
627  // jacobian dI/dV and currents get filled
628  calcDC ();
630 
631  // fill in HB matrices
632  for (int i1 = 0; i1 < 3; i1++) {
633  setQ (i1, _qhs[i1]); // charges
634  setCV (i1, _chs[i1]); // jacobian dQ/dV * V
635  setGV (i1, _ghs[i1]); // jacobian dI/dV * V
636  for (int i2 = 0; i2 < 3; i2++) {
637  setQV (i1, i2, _jdyna[i1][i2]); // jacobian dQ/dV
638  }
639  }
640 }
641 
642 #include "pad3bit.defs.h"