prev=use_mcell_ran4(1)
numSynapses = numCells = 250  	                 
noiseRandom=1				                             //const noise parameters
noiseNumber=1e15
noiseStart=0
seed=netstimSeed=0                               //initialize seed

/*---------------------CREATE SEED FROM SYSTEM DATE--------------------*/
strdef strSeed,strNSSeed	
objref szFunctionsObject		                     //setting date to seed
szFunctionsObject = new StringFunctions()
proc setSeed(){
	system("date +%m%d%H%M%S",strSeed)
	szFunctionsObject.left(strSeed,10)
	sscanf(strSeed,"%lf",&seed)
	
	system("date +%d%H%M%S",strNSSeed)
	szFunctionsObject.left(strNSSeed,9)
	sscanf(strNSSeed,"%lf",&netstimSeed)
}


/*----------------------RANDOM NUMBER GENERATOR------------------------*/
objref r
r = new Random()
//index = r.MCellRan4()
func randNum(){             //generates real number between two arguments
	rand=r.uniform(0,$1)
	return rand
}
func randIntNum(){          //generates integers between two arguments
	rand=r.discunif(0, $1)
	return rand
}


/*----------------------------CREATE CELLS-----------------------------*/
//makeCell(cellindexnumber, %tI)
objectvar cell[numCells], synapse[numSynapses]

proc makeCell(){
    if(cellType==0){
    cell[$1] = new cellA()
  }else if(cellType==1){
    cell[$1] = new cellB()
  }else if(cellType==2){
    cell[$1] = new cellC()
  }else if(cellType==3){
    cell[$1] = new cellD()
  }else {print "uhoh"}
}

/*----------------------CREATE NETWORK WITH NOISE---------------------*/
proc makeNetwork(){
  numI=0                                         //initializes counter for heterogeneous net pop
	//loop creating cells
	for i=0, numCells-1 { makeCell(i) }

	//loop popping on synapses
	for i=0, numCells-1 { 
		cell[i].dendrite synapse[i] = new ExpSyn(1)	
	}
  
	//syntax for connecting cells SThcells[1].soma SThcells[0].nclist.append(new NetCon(&v(1), syn[0], -20, 0.5, synWeight))
	//connect cells
	for i=0, numSynapses-1{
	numConn=radius
	while(numConn>0){
		connCells(i, numConn)
		numConn=numConn-1
		}
	addNoise(i,noiseRandom, noiseNumber, noiseStart, noiseInterval)
	}
}

//add noise process
objectvar pp[numCells]
proc addNoise(){
	pp[$1] = new NetStim()
	pp[$1].noise=$2
	pp[$1].number=$3
	pp[$1].start=$4
	pp[$1].interval=$5
	pp[$1].seed(netstimSeed)
	cell[$1].nclist.append(new NetCon(pp[$1], synapse[$1], -20, .5, 1))
	}


//syntax for netcon: new NetCon(&source_v, synapse, threshold, delay, weight)
proc connCells(){
	if(randNum(1)<p){
		randConnect=randIntNum(numCells-1)
    while (randConnect==$1){
      randConnect=randIntNum(numCells-1)
    }
		cell[$1].soma cell[randConnect].nclist.append(new NetCon(&v(1), synapse[randConnect], -20, 0.5, synWeight))
  }else if(($1+$2)<numCells){
		cell[$1].soma cell[$1+$2].nclist.append(new NetCon(&v(1), synapse[$1+$2], -20, 0.5, synWeight))
  }else{
		cell[$1].soma cell[$1+$2-numCells].nclist.append(new NetCon(&v(1), synapse[$1+$2-numCells], -20, 0.5, synWeight))
  }
	randConnect2=randIntNum(numCells-1)
  while (randConnect2==$1){
    randConnect2=randIntNum(numCells-1)
  }
	if(randNum(1)<p && randConnect2!=randConnect){
		cell[$1].soma cell[randConnect].nclist.append(new NetCon(&v(1), synapse[randConnect], -20, 0.5, synWeight))
  }else if(($1-$2)>=0){
		cell[$1].soma cell[$1-$2].nclist.append(new NetCon(&v(1), synapse[$1-$2], -20, .5, synWeight))
  }else{
		cell[$1].soma cell[numCells+($1-$2)].nclist.append(new NetCon(&v(1), synapse[numCells+($1-$2)], -20, .5, synWeight))
  }
}

objref timevec, idvec, recncs, tobj, nil,indeces
timevec = new Vector()
idvec = new Vector()
recncs = new List()
indeces= new Vector()

/*-----------------CREATE RASTER PLOT AND OUTPUT FILES-----------------*/
objref g, b

proc plotraster(){
  xmin=400
  xmax=600
	g= new Graph()
	g.size(xmin, xmax, 0, numCells)
  indeces.indvwhere(timevec,"[]",xmin, xmax)
  idvec.remove(0,indeces.x[0])
  timevec.remove(0,indeces.x[0])
  idvec.mark(g,timevec,"O",2)

}



/*------------------------PROCS FOR BATCH RUN--------------------------*/
proc myrun(){
b = new VBox()
b.intercept(1)
for(cellType=0; cellType<=3; cellType+=1){
  //setSeed()   //uncomment for seed generated by system datestamp
  newhighindex=r.MCellRan4(seed)
  makeNetwork()
	for i=0,numCells-1 {		//netcons for raster plots and data output 
   cell[i].soma tobj = new NetCon(&v(0.5),nil)
   tobj.record(timevec, idvec, i+1)
   recncs.append(tobj)
	}
		      
	//run and print data
	run()
	plotraster()
}

b.intercept(0)
b.map("From top to bottom: Cells A, B, C and D", 50, 50, 250, 600)
}