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