/* square array of aii amacrines */
/* connected by gap junctions */


timinc = 1e-4;
endexp = 0.50;
crit = 1e-8;
ploti = .001;
relax = .10;
drm = 25000;
tempcel = 22;

arrsiz = 15;			/* size of cone array */

scal = 8;
aiisoma = 7;
drg = 2.5e6;
gjcond =  2e-10;
nconn = 9;

qsize =  .004;
darkv  = -.046;
condr = 12e-10;
rseed = 4669;

aiirest = -.055;

nfv = 4;
vsz = 1;
nfc = 0;

natau = .02;
ktau = .035;

nona = 0;		/* =1 -> no Na, K channels in simulation */
nogj = 0;		/* =1 -> no gap junctions in simulation */
stim1 = 0;		/* =1 -> only 1 AII stimulated (not 5)  */
nostim = 0;		/* =1 -> no stimulus, just resting V */
slowr = 1;		/* =1 -> 100 msec rise time */

nsteps=1;		/* number of steps for stim rise time */
risetime=0.001;		/* length of stim rise time */
stimtime = .15;		/* stim start */
stimlen =  .25;		/* stim duration */

aii = 5;
gangc = 30000;

x = setvar();

if (slowr==1)  {
  risetime = .10;
  nsteps = 100;
};

if (stim1==1) {
   qsize += 0.0017;
};

if (nfv==0) {
   darkv -= .0006;
};

totsiz = arrsiz * arrsiz + 1;

include "mid.m";

midarr = midrow(arrsiz);
mida = mid(arrsiz);

rada = int(arrsiz/2);
cent = rada*scal;			/* location of stimulus in microns */

dim aiiarr[arrsiz][arrsiz][4];
dim gconnect[totsiz][nconn];
dim xx[totsiz];


proc congj(xi,xj,xh,xk) {

source = aiiarr[xi][xj][0];		/* source aii */
dest   = aiiarr[xh][xk][0];		/* dest aii */

found=0;
 for (zi=0; zi1 && j < arrsiz-1 && i>1 && i < arrsiz-1) {
        at [aii][x] chan Na type 0 maxcond 4e-9 tauh=natau;
      };
    };
    if (j>1 && j < arrsiz-1 && i>1 && i < arrsiz-1) {
      at [aii][x] chan K  type 0 maxcond 4e-10 taun=ktau;
    };
    aiiarr[i][j][0] = x++;
  };

if (!nogj) {                            /* make gap junctions betw aii's */
for (j=0; j=arrsiz) continue;
          if (h<0 || h>=arrsiz) continue;
	  if (i!=h && j!=k) continue; 
          if (i==h && j==k) continue;
          congj (i,j,h,k);
      };
};

/*for (i=0; i (rada+1) || i < (rada-1) || i > (rada+1))  
    || (!stim1 && (j!=rada && i!=rada)) || (stim1 && (j!=rada || i!=rada))) { 
     conn [ninp] to [aii][x] synapse open expon=3 thresh=-.045 maxcond=condr
			vrev = -.01 
			vesnoise N=nfv vsize=vsz nfilt2=2 timec2=1.0
			chnoise  N=nfc dur=.0001 ename xx[x];
    }
    else {
     conn [sinp] to [aii][x] synapse open expon=3 thresh=-.045 maxcond=condr
			vrev = -.01
			vesnoise N=nfv vsize=vsz nfilt2=2 timec2=1.0
			chnoise  N=nfc dur=.0001 ename xx[x];

     conn [aii][x] to [gangc] synapse open expon=8 thresh=-.055 maxcond=12e-10
			vrev = -.01
		/*	vesnoise N=5 vsize=2 nfilt2=2 timec2=.5 /* */
		/* 	chnoise  N=100 dur=.0001	/* */;
     ginp++;

    };
  };


/*  printf ("ganglion cell has %g inputs\n",ginp);  /* */

display size 150;
display calibline 10 loc (0.82,0.25);
display center (56,56);
display xrot 60 yrot -10;
display sphere matching [aii][-1];
display sphere matching [aii][mida-1] dscale 1.01 color 4;
display sphere matching [aii][mida]   dscale 1.01 color 4;
display sphere matching [aii][mida+1] dscale 1.01 color 4;
display sphere matching [aii][mida+arrsiz] dscale 1.01 color 4;
display sphere matching [aii][mida-arrsiz] dscale 1.01 color 4;
display gj     matching [aii][-1] dscale -1 color 14;


	/* stimuli */

stim node [ninp] vclamp darkv start 0 dur 1;

stim node [sinp] vclamp darkv start 0 dur stimtime;

stepdur = risetime/nsteps;
topdur = stimlen-2*risetime-.03;

if (nostim==1) {
 stim node [sinp] vclamp=darkv start=stimtime dur stimlen;
}
else {
  for (i=0; i