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