File: d_mos2.model

package info (click to toggle)
gnucap 1%3A20230520-dev-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,836 kB
  • sloc: cpp: 29,956; sh: 352; makefile: 139
file content (661 lines) | stat: -rw-r--r-- 22,195 bytes parent folder | download | duplicates (3)
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
/* $Id: d_mos2.model,v 26.133 2009/11/26 04:58:04 al Exp $ -*- C++ -*-
 * Copyright (C) 2001 Albert Davis
 * Author: Albert Davis <aldavis@gnu.org>
 *
 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *------------------------------------------------------------------
 * mos model equations: spice level 2 equivalent
 */
h_headers {
#include "d_mos123.h"
}
cc_headers {
#include "l_compar.h"
#include "l_denoise.h"
}
/*--------------------------------------------------------------------------*/
model BUILT_IN_MOS2 {
  level 2;
  public_keys {
    nmos2 polarity=pN;
    pmos2 polarity=pP;
  }
  dev_type BUILT_IN_MOS;
  inherit BUILT_IN_MOS123;
  independent {
    override {
      double mjsw "" default=.33;
      double tox "" default=1e-7;
      double cox "" final_default="P_EPS_OX/tox";
      double vto "" final_default=0.0;
      double gamma "" final_default=0.0;
      double phi "" final_default=0.6;
      int mos_level "back-annotate for diode" name=DIODElevel 
	print_test="mos_level != LEVEL" default=LEVEL;
    }
    raw_parameters {
      double kp "transconductance parameter"
	name=KP final_default=2e-5 
	print_test="!calc_kp" calc_print_test="calc_kp";
      double nfs_cm "fast surface state density" 
	name=NFS default=0.0;
      double vmax "max drift velocity of carriers"
	name=VMAx default=NA;
      double neff "total channel charge coefficient"
	name=NEFf default=1.0 positive 
	print_test="neff != 1.0 || lambda == NA";
      double ucrit_cm "critical field mobility degradation"
	name=UCRit default=1e4
	print_test="ucrit_cm != 1e4 || uexp != NA";
      double uexp "critical field exponent in mob.deg."
	name=UEXp default=NA;
      double utra "transverse field coefficient (not used)"
	name=UTRa default=NA print_test=false;
      double delta "width effect on threshold voltage" 
	name=DELta default=0.0;
    }
    calculated_parameters {
      double nfs "" calculate="nfs_cm*ICM2M2";
      double ucrit "" calculate="ucrit_cm*ICM2M";
      bool calc_kp "" default=false;
      double alpha "" calculate="((nsub != NA)
	? (2. * P_EPS_SI) / (P_Q * nsub)
	: 0.)";
      double xd "coeffDepLayWidth" calculate="sqrt(alpha)";
      double xwb "" calculate="((nsub != NA)
	? xd * sqrt(pb)
	: .25e-6)";
      double vbp "" calculate="ucrit * P_EPS_SI / cox";
      double cfsox "" calculate="P_Q * nfs / cox";
    }
    code_pre {      
      if (!has_good_value(tox)) {
	tox = 1e-7;
      }
      cox = P_EPS_OX / tox;
      if (kp == NA) {
	kp = uo * cox;
	calc_kp = true;
      }
      if (nsub != NA) {
	if (phi == NA) {
	  phi = (2. * P_K_Q) * tnom_k * log(nsub/NI);
	  if (phi < .1) {
	    untested();
	    error(((!_sim->is_first_expand()) ? (bDEBUG) : (bWARNING)),
		  long_label() + ": calculated phi too small, using .1\n");
	    phi = .1;
	  }
	  calc_phi = true;
	}
	if (gamma == NA) {
	  gamma = sqrt(2. * P_EPS_SI * P_Q * nsub) / cox;
	  calc_gamma = true;
	}
	if (vto == NA) {
	  double phi_ms = (tpg == gtMETAL)
	    ? polarity * (-.05 - (egap + polarity * phi) / 2.)
	    : -(tpg * egap + phi) / 2.;
	  double vfb = phi_ms - polarity * P_Q * nss / cox;
	  vto = vfb + phi + gamma * sqrt(phi);
	  calc_vto = true;
	}
      }
    }
  }
  size_dependent {
    calculated_parameters {
      double relxj "" calculate="((m->xj != NA && m->xj > 0)
	       ? .5 * m->xj / l_eff
	       : NA)";
      double eta_1 "" calculate="((cgate != 0)
	       ? M_PI_4 * P_EPS_SI * m->delta / cgate * l_eff
	       : 0.)";
      double eta "" calculate="eta_1 + 1.";
      double eta_2 "" calculate="eta / 2.";
    }
  }
  temperature_dependent {
    calculated_parameters {
      double vt "" calculate="temp * P_K_Q";
      double phi "" calculate="m->phi*tempratio + (-2*vt*(1.5*log(tempratio)+P_Q*(arg)))";
      double sqrt_phi "" calculate="sqrt(phi)";
      double phi_sqrt_phi "" calculate="phi * sqrt_phi";
      double beta "" calculate="(m->kp / tempratio4) * s->w_eff / s->l_eff";
      double uo "" calculate="m->uo * tempratio4";
      double vbi "" calculate="(fixzero(
	(m->vto - m->gamma * sqrt(m->phi)
	 +.5*(m->egap-egap) + m->polarity* .5 * (phi-m->phi)), m->phi))";
    }
    code_pre {
      double temp = d->_sim->_temp_c + P_CELSIUS0;
      double tempratio  = temp / m->tnom_k; // ratio
      double tempratio4 = tempratio * sqrt(tempratio);
      double kt = temp * P_K;
      double egap = 1.16 - (7.02e-4*temp*temp) / (temp+1108.);
      double arg = (m->egap*tempratio - egap) / (2*kt);
    }
  }
  /*-----------------------------------------------------------------------*/
  tr_eval {
    #define short_channel	(m->xj != NOT_INPUT  &&  m->xj > 0.)
    #define do_subthreshold	(m->nfs != 0.)
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    trace0(d->long_label().c_str());
    trace3("", d->vds, d->vgs, d->vbs);
    assert(m->tnom_k > 0);
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    d->reverse_if_needed();
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    double v_phi_s = t->phi - d->vbs;
    double sarg, dsarg_dvbs, d2sdb2, sarg3;
    {
      if (d->vbs <= 0.) {
	sarg = sqrt(v_phi_s);
	dsarg_dvbs = -.5 / sarg;
	d2sdb2 = .5 * dsarg_dvbs / v_phi_s;
	d->sbfwd = false;
	trace3("sb-ok", sarg, v_phi_s, dsarg_dvbs);
      }else{
	if (OPT::mosflags & 01000) {
	  sarg = t->sqrt_phi / (1. + .5 * d->vbs / t->phi);
	  dsarg_dvbs = -.5 * sarg * sarg / t->phi_sqrt_phi;
	  d2sdb2 = -dsarg_dvbs * sarg / t->phi_sqrt_phi;
	  untested();
	  trace3("***sb-reversed(01000)***", sarg, v_phi_s, dsarg_dvbs);
	}else{
	  sarg = t->sqrt_phi 
	    / (1. + .5 * d->vbs / t->phi 
	       + .375 * d->vbs * d->vbs / (t->phi * t->phi));
	  dsarg_dvbs = (-.5 * sarg * sarg / t->phi_sqrt_phi) 
	    * (1. + 1.5 * d->vbs / t->phi);
	  d2sdb2 = (-dsarg_dvbs * sarg / t->phi_sqrt_phi)
	    - (.75 * sarg / (t->phi_sqrt_phi * t->phi)) 
	    * (2. * d->vbs * dsarg_dvbs + sarg);
	  untested();
	  trace3("***sb-reversed(00000)***", sarg, v_phi_s, dsarg_dvbs);
	}
	d->sbfwd = true;
      }
      sarg3 = sarg*sarg*sarg;
      assert(sarg > 0.);
      assert(dsarg_dvbs < 0.);
      assert(up_order(-1/t->phi, d2sdb2, 1/t->phi));
      trace2("", d2sdb2, sarg3);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    double barg, dbarg_dvbs, d2bdb2;
    {
      double vbd = d->vbs - d->vds;
      double v_phi_d = t->phi - vbd;
      if (vbd <= 0.) {
	barg = sqrt(v_phi_d);
	dbarg_dvbs = -.5 / barg;
	d2bdb2 = .5 * dbarg_dvbs / v_phi_d;
	//d->dbfwd = false;
	trace4("db-ok", barg, v_phi_d, dbarg_dvbs, d2bdb2);
      }else{
	if (OPT::mosflags & 01000) {
	  barg = t->sqrt_phi / (1. + .5 * vbd / t->phi);
	  dbarg_dvbs = -.5 * barg * barg / t->phi_sqrt_phi;
	  d2bdb2 = -dbarg_dvbs * barg / t->phi_sqrt_phi;
	  untested();
	  trace4("***db-reversed(00000)***",barg, v_phi_d, dbarg_dvbs, d2bdb2);
	}else{
	  barg = t->sqrt_phi 
	    / (1. + .5 * vbd / t->phi 
	       + .375 * vbd * vbd / (t->phi * t->phi));
	  dbarg_dvbs = (-.5 * barg * barg / t->phi_sqrt_phi) 
	    * (1. + 1.5 * vbd / t->phi);
	  d2bdb2 = (-dbarg_dvbs * barg / t->phi_sqrt_phi)
	    - (.75 * barg / (t->phi_sqrt_phi * t->phi)) 
	    * (2. * vbd * dbarg_dvbs + barg);
	  trace4("***db-reversed(00000)***",barg, v_phi_d, dbarg_dvbs, d2bdb2);
	}
	//d->dbfwd = true;
      }
      assert(barg > 0.);
      assert(dbarg_dvbs < 0.);
      assert(up_order(-1/t->phi, d2bdb2, 1/t->phi));
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    double gamma_s, dgamma_s_dvds, dgamma_s_dvbs, dgddb2;
    {
      if (short_channel) {
	double argxd = 1. + 2. * barg * m->xd / m->xj;
	assert(argxd > 0);
	double argd = sqrt(argxd);
	trace2("", argxd, argd);
	
	double alpha_d = s->relxj * (argd - 1.);
	double dalpha_d_dvds = m->xd / (4. * s->l_eff * argd * barg);
	double dalpha_d_dvbs = -dalpha_d_dvds;
	trace3("", alpha_d, dalpha_d_dvds, dalpha_d_dvbs);
	
	double argxs = 1. + 2. * sarg * m->xd / m->xj;
	assert(argxs > 0);
	double args = sqrt(argxs);
	trace2("", argxs, args);
	
	double alpha_s = s->relxj * (args - 1.);
	double dalpha_s_dvbs = -m->xd / (4. * s->l_eff * args * sarg);
	trace2("", alpha_s, dalpha_s_dvbs);
	
	gamma_s = m->gamma * (1. - alpha_s - alpha_d);
	dgamma_s_dvds = -m->gamma *  dalpha_d_dvds;
	dgamma_s_dvbs = -m->gamma * (dalpha_d_dvbs + dalpha_s_dvbs);
	
	double dasdb2=-m->xd*(d2sdb2+dsarg_dvbs*dsarg_dvbs*m->xd/(m->xj*argxs))
	  / (s->l_eff*args);
	double daddb2=-m->xd*(d2bdb2+dbarg_dvbs*dbarg_dvbs*m->xd/(m->xj*argxd))
	  / (s->l_eff*argd);
	dgddb2 = -.5 * m->gamma * (dasdb2 + daddb2);
	
	if (gamma_s <= 0. && m->gamma > 0.) {
	  untested();
	  error(bTRACE, d->long_label() + ": gamma is negative\n");
	  error(bTRACE, "+   gamma_s=%g, alpha_s=%g, alpha_d=%g\n",
		gamma_s,    alpha_s,    alpha_d);
	}
	trace4("no short chan", gamma_s, dgamma_s_dvds, dgamma_s_dvds, dgddb2);
      }else{
	gamma_s = m->gamma;
	dgamma_s_dvds = dgamma_s_dvbs = 0.;
	dgddb2 = 0.;
	trace4("short channel", gamma_s, dgamma_s_dvds, dgamma_s_dvds, dgddb2);
      }
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    /* von, subthreshold, cutoff, vgst */
    double vc, vc_eta, dvon_dvbs;
    double xn, vtxn, dxn_dvbs;	/* subthreshold only */
    {
      double vbin = t->vbi + s->eta_1 * v_phi_s;
      d->von = vbin + gamma_s * sarg;
      dvon_dvbs = -s->eta_1 + dgamma_s_dvbs * sarg + gamma_s * dsarg_dvbs;
      trace3("guess", vbin, d->von, dvon_dvbs);
      
      if (do_subthreshold) {
	double cdonco = -(gamma_s*dsarg_dvbs + dgamma_s_dvbs*sarg) + s->eta_1;
	xn = 1. + m->cfsox + cdonco;
	vtxn = t->vt * xn;
	dxn_dvbs = 2. * dgamma_s_dvbs * dsarg_dvbs
	  + gamma_s * d2sdb2 + dgddb2 * sarg;
	trace3("do_sub", xn, vtxn, dxn_dvbs);
	
	d->von += vtxn;
	dvon_dvbs += t->vt * dxn_dvbs;
	d->vgst = d->vgs - d->von;
	trace3("", d->von, dvon_dvbs, d->vgst);
	
	d->subthreshold = (d->vgs < d->von);
	d->cutoff = false;
      }else{
	xn = vtxn = dxn_dvbs = 0.;
	d->vgst = d->vgs - d->von;
	trace3("no_sub", xn, vtxn, dxn_dvbs);
	trace3("", d->von, dvon_dvbs, d->vgst);
	
	d->subthreshold = false;
	d->cutoff = (d->vgs < d->von);
	if (d->cutoff) {
	  trace0("***** cut off *****");
	  d->ids = 0.;
	  d->gmf = d->gmr = 0.;
	  d->gds = 0.;
	  d->gmbf = d->gmbr = 0.;
	  return;
	}
      }
      double vgsx = (d->subthreshold) ? d->von : d->vgs;
      vc = vgsx - vbin;
      vc_eta = vc / s->eta;
      trace3("", vgsx, vc, vc_eta);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 

    double ufact, duf_dvgs, duf_dvds, duf_dvbs, ueff;
    {
      if (m->uexp != NOT_INPUT  &&  d->vgst > m->vbp) {
	ufact = pow(m->vbp/d->vgst, m->uexp);
	duf_dvgs = -ufact * m->uexp / d->vgst;
	duf_dvds = 0.;	/* wrong, but as per spice2 */
	duf_dvbs = dvon_dvbs * ufact * m->uexp / d->vgst;
	trace4("calc ufact", ufact, duf_dvgs, duf_dvds, duf_dvbs);
      }else{
	ufact = 1.;
	duf_dvgs = duf_dvds = duf_dvbs = 0.;
	trace4("def ufact", ufact, duf_dvgs, duf_dvds, duf_dvbs);
      }
      ueff = t->uo * ufact;	/* ???? */
      trace2("", ufact, ueff);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    /* vdsat  according to Baum's Theory of scattering velocity saturation  */
    int use_vmax = m->vmax != NOT_INPUT;
    if (use_vmax) {
      double gammad = gamma_s / s->eta;
      double v1 = vc_eta + v_phi_s;
      double v2 = v_phi_s;
      double xv = m->vmax * s->l_eff / ueff;
      double a1 = gammad * (4./3.);
      double b1 = -2. * (v1+xv);
      double c1 = -2. * gammad * xv;			/* end of scope */
      double d1 = 2.*v1*(v2+xv) - v2*v2 - (4./3.)*gammad*sarg3;
      double a = -b1;					/* xv, v1, v2, sarg3 */
      double b = a1 * c1 - 4. * d1;
      double C = -d1 * (a1*a1 - 4.*b1) - c1*c1;
      double r = -a*a / 3. + b;
      double r3 = r*r*r;				/* r */
      double S = 2. * a*a*a / 27. - a*b / 3. + C;	/* b, c */
      double s2 = S*S;
      double p = s2 / 4. + r3 / 27.;			/* r */
      double y3;
      if (p < 0.) {					/* p */
	double ro = pow((-r3 / 27), (1./6.));		/* s2, r3 */
	double fi = atan(-2. * sqrt(-p) / S);
	y3 = 2. * ro * cos(fi/3.) - a / 3.;
      }else{
	double p2 = sqrt(p);
	double p3 = pow((fabs(-S/2.+p2)), (1./3.));
	double p4 = pow((fabs(-S/2.-p2)), (1./3.));	/* s */
	y3 = p3 + p4 - a / 3.;				/* a */
	untested();
      }
      
      double x4[8];
      int iknt = 0;
      if (a1*a1 / 4. - b1 + y3  < 0.  &&  y3*y3 / 4. - d1  < 0.) {
	untested();
	error(bWARNING,
	      "%s: internal error: a3,b4, a1=%g, b1=%g, y3=%g, d1=%g\n",
	      d->long_label().c_str(),    a1,    b1,    y3,    d1);
      }else{
	double a3 = sqrt(a1*a1 / 4. - b1 + y3);
	double b3 = sqrt(y3*y3 / 4. - d1);
	for (int i = 0;   i < 4;   i++) {
	  static const double sig1[4] = {1., -1., 1., -1.};
	  static const double sig2[4] = {1., 1., -1., -1.};
	  double a4 = a1 / 2. + sig1[i] * a3;
	  double b4 = y3 / 2. + sig2[i] * b3;		/* y3 */
	  double delta4 = a4*a4 / 4. - b4;
	  if (delta4 >= 0.) {
	    double sd4 = sqrt(delta4);
	    x4[iknt++] = - a4 / 2. + sd4;
	    x4[iknt++] = - a4 / 2. - sd4;		/* i */
	  }
	}
      }
      
      double xvalid = 0.;
      int root_count = 0;
      for (int j = 0;   j < iknt;   j++) {			/* iknt */
	if (x4[j] > 0.) {
	  double poly4 = x4[j]*x4[j]*x4[j]*x4[j]/* ~= 0, used as check	*/
	    + a1 * x4[j]*x4[j]*x4[j]		/* roundoff error not	*/
	    + b1 * x4[j]*x4[j]			/* propagated, so ok	*/
	    + c1 * x4[j]
	    + d1;				/* a1, b1, c1, d1 */
	  if (fabs(poly4) <= 1e-6) {
	    root_count++;
	    if (root_count <= 1) {		/* xvalid = min(x4[j]) */
	      xvalid=x4[j];
	    }
	    if (x4[j] <= xvalid) {
	      xvalid=x4[j];				/* x4[], j */
	    }else{
	      untested();
	    }
	  }
	}
      }
      if (root_count <= 0) {				/* root_count */
	error(bTRACE, d->long_label() + ": Baum's theory rejected\n");
	use_vmax = false;
	d->vdsat = 0.;
	trace1("use_vmax rejected", d->vdsat);
      }else{
	d->vdsat = xvalid*xvalid - v_phi_s;
	trace1("use_vmax", d->vdsat);
      }
    }else{
      d->vdsat = 0.;
      trace1("!use_vmax", d->vdsat);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    /* vdsat     according to Grove-Frohman equation  */
    double dvdsat_dvgs = NOT_VALID;
    double dvdsat_dvbs = NOT_VALID;
    if (!use_vmax) {
      if (gamma_s > 0.) {
	double argv = vc_eta + v_phi_s;
	if (argv > 0.) {
	  double gammad = gamma_s / s->eta;
	  double gammd2 = gammad * gammad;
	  double arg1 = sqrt(1. + 4. * argv / gammd2);
	  d->vdsat = vc_eta  +  gammd2 * (1.-arg1) / 2.;
	  dvdsat_dvgs = (1. - 1./arg1) / s->eta;
	  dvdsat_dvbs = (gammad * (1.-arg1) + 2.*argv / (gammad*arg1))
	    / s->eta * dgamma_s_dvbs
	    + 1./arg1 + s->eta_1 * dvdsat_dvgs;
	  trace3("!use_vmax,gamma>0,argv>0",d->vdsat,dvdsat_dvgs,dvdsat_dvbs);
	}else{
	  untested();
	  d->vdsat = 0.;
	  dvdsat_dvgs = dvdsat_dvbs = 0.;
	  error(bTRACE, d->long_label() + ": argv is negative\n");
	  trace2("argv<0", argv, vc);
	  trace3("!use_vmax,gamma>0,argv<=0",d->vdsat,dvdsat_dvgs,dvdsat_dvbs);
	}
      }else{
	d->vdsat = vc_eta;
	dvdsat_dvgs = 1.;
	dvdsat_dvbs = 0.;
	trace3("!use_vmax, gamma<=0", d->vdsat, dvdsat_dvgs, dvdsat_dvbs);
      }
    }else{
      /* dvdsat_dvgs, dvdsat_dvbs   deferred */
      trace3("use_vmax", d->vdsat, dvdsat_dvgs, dvdsat_dvbs);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    if (d->vdsat < 0.) {
      error(bWARNING,
	    "%s: calculated vdsat (%g) < 0.  using vdsat = 0.\n",
	    d->long_label().c_str(), d->vdsat);
      d->vdsat = 0.;
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    double bsarg, dbsarg_dvbs;
    {
      double vbdsat = d->vbs - d->vdsat;
      if (vbdsat <= 0.) {
	double v_phi_ds = t->phi - vbdsat;
	bsarg = sqrt(v_phi_ds);
	dbsarg_dvbs = -.5 / bsarg;
	trace3("vbdsat <= 0", vbdsat, bsarg, dbsarg_dvbs);
      }else{
	bsarg = t->sqrt_phi / (1. + .5 * vbdsat / t->phi);
	dbsarg_dvbs = -.5 * bsarg * bsarg / t->phi_sqrt_phi;
	untested();
	trace3("vbdsat > 0", vbdsat, bsarg, dbsarg_dvbs);
      }
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    /* local   dvdsat_dvgs, dvdsat_dvbs   maybe */
    {
      if (use_vmax) {
	double bodys = bsarg*bsarg*bsarg - sarg3;
	double gdbdvs =
	  2. * gamma_s * (bsarg*bsarg*dbsarg_dvbs - sarg*sarg*dsarg_dvbs);
	double argv = vc_eta - d->vdsat;
	double vqchan = argv - gamma_s * bsarg;
	double dqdsat = -1. + gamma_s * dbsarg_dvbs;
	double vl = m->vmax * s->l_eff;
	double dfunds = vl * dqdsat - ueff * vqchan;
	double dfundg = (vl - ueff * d->vdsat) / s->eta;
	double dfundb = -vl * (1. + dqdsat - s->eta_1 / s->eta)
	  + ueff * (gdbdvs - dgamma_s_dvbs * bodys / 1.5) / s->eta;
	dvdsat_dvgs = -dfundg / dfunds;
	dvdsat_dvbs = -dfundb / dfunds;
	trace2("use_vmax", dvdsat_dvgs, dvdsat_dvbs);
      }else{
	/* dvdsat_dvgs, dvdsat_dvbs   already set */
	trace2("!use_vmax", dvdsat_dvgs, dvdsat_dvbs);
      }
      assert(dvdsat_dvgs != NOT_VALID);
      assert(dvdsat_dvbs != NOT_VALID);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    double  dl_dvgs, dl_dvds, dl_dvbs, clfact;
    {
      if (d->vds >= 0.) {
	if (m->lambda == NOT_INPUT) {
	  double dldsat;
	  if (use_vmax) {
	    double xdv = m->xd / sqrt(m->neff);
	    double xlv = m->vmax * xdv / (2. * ueff);
	    double argv = d->vds - d->vdsat;
	    if (argv < 0.) {
	      argv = 0.;
	    }
	    double xls = sqrt(xlv*xlv + argv);
	    double dl = (xls-xlv) * xdv;
	    /* lambda = dl / (s->l_eff * d->vds); */
	    clfact = (1. - dl / s->l_eff);
	    dldsat = xdv / (2. * xls * s->l_eff);
	  }else{
	    double argv = (d->vds - d->vdsat) / 4.;
	    double sargv = sqrt(1. + argv*argv);
	    if (argv + sargv >= 0.) {
	      double dl = m->xd * sqrt(argv + sargv);
	      /* lambda = dl / (s->l_eff * d->vds); */
	      clfact = (1. - dl / s->l_eff);
	      /* dldsat = lambda * d->vds / (8. * sargv); */
	      dldsat = dl / (s->l_eff * 8. * sargv);
	    }else{
	      /* lambda = 0.; */
	      clfact = 1.;
	      dldsat = 0.;
	      untested();
	      error(bWARNING,
		    "%s: internal error: vds(%g) < vdsat(%g)\n",
		    d->long_label().c_str(), d->vds,   d->vdsat);
	    }
	  }
	  dl_dvgs =  dvdsat_dvgs * dldsat;
	  dl_dvds =              - dldsat;
	  dl_dvbs =  dvdsat_dvbs * dldsat;
	}else{
	  /* lambda = m->lambda; */
	  clfact = (1. - m->lambda * d->vds);
	  dl_dvgs = dl_dvbs = 0.;
	  dl_dvds = -m->lambda;
	}
	
	/* clfact = (1. - lambda * d->vds); */
	if (clfact < m->xwb/s->l_eff) {
	  double leff = m->xwb / (2. - (clfact * s->l_eff / m->xwb));
	  double dfact = (leff * leff) / (m->xwb * m->xwb);
	  clfact = leff / s->l_eff;
	  dl_dvgs *= dfact;
	  dl_dvds *= dfact;
	  dl_dvbs *= dfact;
	}
      }else{  /* vds <= 0. */
	/* lambda = 0.; */
	clfact = 1.;
	dl_dvgs = dl_dvds = dl_dvbs = 0.;
	trace1("*** vds < 0 ***", d->vds);
      }
      trace4("", dl_dvgs, dl_dvds, dl_dvbs, clfact);
    }
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
    /* ids, gmf, gds, gmbf */
    {
      d->saturated = (d->vds > d->vdsat);
      double vdsx =  (d->saturated) ? d->vdsat : d->vds;
      double bargx = (d->saturated) ? bsarg : barg;
      double body = bargx*bargx*bargx - sarg3;
      double expg = (d->subthreshold) ? exp(d->vgst / vtxn) : 1.;
      trace4("", vdsx, bargx, body, expg);
      
      trace3("", t->beta, ufact, clfact);
      double beta = t->beta * ufact / clfact;
      double ids_on = 
	beta * ((vc - s->eta_2 * vdsx) * vdsx  - (2./3.) * gamma_s * body);
      trace4("", beta, vc, (s->eta*vdsx),  (gamma_s*bargx));
      double didvds = beta * (vc  -  s->eta * vdsx  -  gamma_s * bargx);
      fixzero(&didvds, ids_on);
      trace4("", beta, ids_on, didvds, d->saturated);
      
      d->ids = ids_on * expg;
      
      d->gmf = beta * vdsx;
      d->gmf += ids_on * (duf_dvgs/ufact - dl_dvgs/clfact);
      if (d->saturated) {
	d->gmf += didvds * dvdsat_dvgs;
      }
      if (d->subthreshold) {
	d->gmf = ids_on / vtxn;
	if (d->saturated) {
	  d->gmf += didvds * dvdsat_dvgs;
	}
	d->gmf *= expg;
      }
      
      d->gds = (d->saturated) ? 0.: didvds;
      d->gds += ids_on * (duf_dvds/ufact - dl_dvds/clfact);
      if (short_channel) {
	d->gds -= beta * (2./3.) * body * dgamma_s_dvds;
      }
      if (d->subthreshold) {
	double dxndvd = dgamma_s_dvds * dsarg_dvbs;
	double dodvds = dgamma_s_dvds * sarg + t->vt * dxndvd;
	double gmw = d->ids * d->vgst / (vtxn * xn);
	d->gds *= expg;
	d->gds -= d->gmf * dodvds + gmw * dxndvd;
      }
      
      d->gmbf = beta * (s->eta_1 * vdsx - gamma_s * (sarg - bargx));
      d->gmbf += ids_on * (duf_dvbs/ufact - dl_dvbs/clfact);
      if (short_channel) {
	d->gmbf -= beta * (2./3.) * body * dgamma_s_dvbs;
      }
      if (d->saturated) {
	d->gmbf += didvds * dvdsat_dvbs;
      }
      if (d->subthreshold) {
	double gmw = d->ids * d->vgst / (vtxn * xn);
	d->gmbf += beta * dvon_dvbs * vdsx;
	d->gmbf *= expg;
	d->gmbf -= d->gmf * dvon_dvbs + gmw * dxn_dvbs;
      }
      trace4("", d->ids, d->gmf, d->gds, d->gmbf);
    }
    if (d->reversed) {
      d->ids *= -1;
      d->gmr = d->gmf;
      d->gmbr = d->gmbf;
      d->gmf = d->gmbf = 0;
    }else{
      d->gmr = d->gmbr = 0.;
    }
  }
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/