/* mosq2.f -- translated by f2c (version of 3 February 1990  3:36:42).
   You must link the resulting object file with the libraries:
	-lF77 -lI77 -lm -lc   (in that order)
*/

#include "f2c.h"

/*<       subroutine mosq2(vds,vbs,vgs,vdsat,vth,vbin,gamasd,cox,phi, >*/
/*<      1   qg,qc,qb,cggb,cgdb,cgsb,cbgb,cbdb,cbsb) >*/
/* Subroutine */ int mosq2_(vds, vbs, vgs, vdsat, vth, vbin, gamasd, cox, phi,
	 qg, qc, qb, cggb, cgdb, cgsb, cbgb, cbdb, cbsb)
doublereal *vds, *vbs, *vgs, *vdsat, *vth, *vbin, *gamasd, *cox, *phi, *qg, *
	qc, *qb, *cggb, *cgdb, *cgsb, *cbgb, *cbdb, *cbsb;
{
    /* System generated locals */
    doublereal d_1;

    /* Builtin functions */
    double sqrt();

    /* Local variables */
    static doublereal argd, argn, ve1p5, ve2p5, vsat, vs1p5, vs2p5, argd2, 
	    term0, term1, term2, term3, term4, term5, term6, term7, dddve, 
	    dddvs, term10, term11, term12, term20, term21, term22, sqarg, 
	    gamma2, vd, ve, vg, dgndve, vs, dqgdve, dvedvd, dvedvg, dgndvs, 
	    ve2, ve3, ve5, vs2, vs3, vs5, vbd, vgb, vep5, vsp5;

/*<       implicit double precision (a-h,o-z) >*/

/*     initialize charges; */
/*     change reference voltages for charge computation */

/*<       qg=0.0d0 >*/
    *qg = 0.;
/*<       qb=0.0d0 >*/
    *qb = 0.;
/*<       vbd=vbs-vds >*/
    vbd = *vbs - *vds;
/*<       vgb=vgs-vbs >*/
    vgb = *vgs - *vbs;
/*<       vd=dmax1(phi-vbd,1.0d-8) >*/
/* Computing MAX */
    d_1 = *phi - vbd;
    vd = max(1e-8,d_1);
/*<       vs=dmax1(phi-vbs,1.0d-8) >*/
/* Computing MAX */
    d_1 = *phi - *vbs;
    vs = max(1e-8,d_1);
/*<       vg=vgb-vbin+phi >*/
    vg = vgb - *vbin + *phi;
/*<       vsp5=dsqrt(vs) >*/
    vsp5 = sqrt(vs);

/*     determine operating region */

/*<       if (vgs.le.vth) go to 1100 >*/
    if (*vgs <= *vth) {
	goto L1100;
    }

/*     compute charges for "on" region */

/*<  1020 vsat=vdsat+vs >*/
/* L1020: */
    vsat = *vdsat + vs;
/*<       vs2=vs*vs >*/
    vs2 = vs * vs;
/*<       vs3=vs2*vs >*/
    vs3 = vs2 * vs;
/*<       vs5=vs3*vs2 >*/
    vs5 = vs3 * vs2;
/*<       vs1p5=vs*vsp5 >*/
    vs1p5 = vs * vsp5;
/*<       vs2p5=vs1p5*vs >*/
    vs2p5 = vs1p5 * vs;
/*<  1025 if (vd.ge.vsat) go to 1035 >*/
/* L1025: */
    if (vd >= vsat) {
	goto L1035;
    }
/*<       ve=vd >*/
    ve = vd;
/*<  1030 dvedvd=1.0d0 >*/
/* L1030: */
    dvedvd = 1.;
/*<       dvedvg=0.0d0 >*/
    dvedvg = 0.;
/*<       go to 1040 >*/
    goto L1040;
/*<  1035 ve=vsat >*/
L1035:
    ve = vsat;
/*<       dvedvd=0.0d0 >*/
    dvedvd = 0.;
/*<       dvedvg=0.0d0 >*/
    dvedvg = 0.;
/*<  1040 ve2=ve*ve >*/
L1040:
    ve2 = ve * ve;
/*<       ve3=ve2*ve >*/
    ve3 = ve2 * ve;
/*<       ve5=ve2*ve3 >*/
    ve5 = ve2 * ve3;
/*<       vep5=dsqrt(ve) >*/
    vep5 = sqrt(ve);
/*<       ve1p5=ve*vep5 >*/
    ve1p5 = ve * vep5;
/*<       ve2p5=ve1p5*ve >*/
    ve2p5 = ve1p5 * ve;
/*<       term0=ve+vs >*/
    term0 = ve + vs;
/*<       term1=vep5+vsp5 >*/
    term1 = vep5 + vsp5;
/*<       term2=vep5*vsp5 >*/
    term2 = vep5 * vsp5;
/*<       term3=ve2+vs2 >*/
    term3 = ve2 + vs2;
/*<       term4=ve*vs >*/
    term4 = ve * vs;
/*<       term5=term0*term1 >*/
    term5 = term0 * term1;
/*<       term6=(term3+term4)+term2*term0 >*/
    term6 = term3 + term4 + term2 * term0;
/*<       term7=(term3+term4)*term1 >*/
    term7 = (term3 + term4) * term1;
/*<       term10=vep5+0.5d0*vsp5 >*/
    term10 = vep5 + vsp5 * .5;
/*<       term11=1.5d0*ve+vsp5*term10 >*/
    term11 = ve * 1.5 + vsp5 * term10;
/*<       term12=2.0d0*ve1p5+vsp5*term11 >*/
    term12 = ve1p5 * 2. + vsp5 * term11;
/*<       term20=0.5d0*vep5+vsp5 >*/
    term20 = vep5 * .5 + vsp5;
/*<       term21=1.5d0*vs+vep5*term20 >*/
    term21 = vs * 1.5 + vep5 * term20;
/*<       term22=2.0d0*vs1p5+vep5*term21 >*/
    term22 = vs1p5 * 2. + vep5 * term21;
/*<       argn=0.5d0*vg*term5-0.4d0*gamasd*term6-term7/3.0d0 >*/
    argn = vg * .5 * term5 - *gamasd * .4 * term6 - term7 / 3.;
/*<       argd=vg*term1-gamasd*(term0+term2)/1.5d0-0.5d0*term1*term0 >*/
    argd = vg * term1 - *gamasd * (term0 + term2) / 1.5 - term1 * .5 * term0;
/*<       argd2=argd*argd >*/
    argd2 = argd * argd;
/*<       qg=cox*(vg-argn/argd) >*/
    *qg = *cox * (vg - argn / argd);
/*<       dgndve=0.5d0*vg*term11-0.4d0*gamasd*term12- >*/
/*<      1   (2.5d0*ve2+vsp5*term12)/3.0d0 >*/
    dgndve = vg * .5 * term11 - *gamasd * .4 * term12 - (ve2 * 2.5 + vsp5 * 
	    term12) / 3.;
/*<       dddve=0.5d0*vg-gamasd*term10/1.5d0-0.5d0*term11 >*/
    dddve = vg * .5 - *gamasd * term10 / 1.5 - term11 * .5;
/*<       dqgdve=-cox/argd*(dgndve-(vg-qg/cox)*dddve) >*/
    dqgdve = -(*cox) / argd * (dgndve - (vg - *qg / *cox) * dddve);
/*<       dgndvs=0.5d0*vg*term21-0.4d0*gamasd*term22- >*/
/*<      1   (2.5d0*vs2+vep5*term22)/3.0d0 >*/
    dgndvs = vg * .5 * term21 - *gamasd * .4 * term22 - (vs2 * 2.5 + vep5 * 
	    term22) / 3.;
/*<       dddvs=0.5d0*vg-gamasd*term20/1.5d0-0.5d0*term21 >*/
    dddvs = vg * .5 - *gamasd * term20 / 1.5 - term21 * .5;
/*<       cgdb=-cox/(argd*vep5)*(dgndve-(vg-qg/cox)*dddve)*dvedvd >*/
    *cgdb = -(*cox) / (argd * vep5) * (dgndve - (vg - *qg / *cox) * dddve) * 
	    dvedvd;
/*<       cgsb=-cox/(argd*vsp5)*(dgndvs-(vg-qg/cox)*dddvs) >*/
    *cgsb = -(*cox) / (argd * vsp5) * (dgndvs - (vg - *qg / *cox) * dddvs);
/*<       cggb=cox*(1.0d0-term1/argd*(0.5d0*term0-vg+qg/cox)) >*/
    *cggb = *cox * (1. - term1 / argd * (term0 * .5 - vg + *qg / *cox));
/*<       argn=vg*(term0+term2)/1.5d0-0.5d0*gamasd*term5-0.4d0*term6 >*/
    argn = vg * (term0 + term2) / 1.5 - *gamasd * .5 * term5 - term6 * .4;
/*<       dgndve=vg*term10/1.5d0-0.5d0*gamasd*term11-0.4d0*term12 >*/
    dgndve = vg * term10 / 1.5 - *gamasd * .5 * term11 - term12 * .4;
/*<       dgndvs=vg*term20/1.5d0-0.5d0*gamasd*term21-0.4d0*term22 >*/
    dgndvs = vg * term20 / 1.5 - *gamasd * .5 * term21 - term22 * .4;
/*<       qb=-gamasd*cox*argn/argd >*/
    *qb = -(*gamasd) * *cox * argn / argd;
/*<       cbdb=-cox/(vep5*argd)*(qb/cox*dddve+gamasd*dgndve)*dvedvd >*/
    *cbdb = -(*cox) / (vep5 * argd) * (*qb / *cox * dddve + *gamasd * dgndve) 
	    * dvedvd;
/*<       cbsb=-cox/(vsp5*argd)*(qb/cox*dddvs+gamasd*dgndvs) >*/
    *cbsb = -(*cox) / (vsp5 * argd) * (*qb / *cox * dddvs + *gamasd * dgndvs);

/*<       cbgb=-cox/argd*(gamasd*(term0+term2)/1.5d0+qb/cox*term1) >*/
    *cbgb = -(*cox) / argd * (*gamasd * (term0 + term2) / 1.5 + *qb / *cox * 
	    term1);
/*<       go to 2000 >*/
    goto L2000;

/*  finish special cases */

/*<  1100 if (vg.gt.0.0d0) go to 1110 >*/
L1100:
    if (vg > 0.) {
	goto L1110;
    }
/*<       qg=cox*vg >*/
    *qg = *cox * vg;
/*<       cggb=cox >*/
    *cggb = *cox;
/*<       go to 1120 >*/
    goto L1120;
/*<  1110 gamma2=gamasd*0.5d0 >*/
L1110:
    gamma2 = *gamasd * .5;
/*<       sqarg=dsqrt(gamma2*gamma2+vg) >*/
    sqarg = sqrt(gamma2 * gamma2 + vg);
/*<       qg=gamasd*cox*(sqarg-gamma2) >*/
    *qg = *gamasd * *cox * (sqarg - gamma2);
/*<       cggb=0.5d0*cox*gamasd/sqarg >*/
    *cggb = *cox * .5 * *gamasd / sqarg;
/*<  1120 qb=-qg >*/
L1120:
    *qb = -(*qg);
/*<       cbgb=-cggb >*/
    *cbgb = -(*cggb);
/*<       cgdb=0.0d0 >*/
    *cgdb = 0.;
/*<       cgsb=0.0d0 >*/
    *cgsb = 0.;
/*<       cbdb=0.0d0 >*/
    *cbdb = 0.;
/*<       cbsb=0.0d0 >*/
    *cbsb = 0.;

/*  finished */

/*<  2000 qc=-(qg+qb) >*/
L2000:
    *qc = -(*qg + *qb);
/*<  2050 return >*/
/* L2050: */
    return 0;
/*<       end >*/
} /* mosq2_ */

