// Calculatin of the connectivity and conduction delays
//
// Written by Shyam Kumar Sudhakar, Ivan Raikov, Tom Close, Rodrigo Publio, Daqing Guo, and Sungho Hong
// Computational Neuroscience Unit, Okinawa Institute of Science and Technology, Japan
// Supervisor: Erik De Schutter
//
// Correspondence: Sungho Hong (shhong@oist.jp)
//
// September 16, 2017

begintemplate enPassage

public connectPops1,glomdistance,Nmean, MFGoCtotal,MFGCtotal,PFtotal,Axontotal, activeGC,PCidentitiesforBC,PCBCdistance,PCidentitiesforSC,PCSCdistance,PCidentitiesforBC,PCSCdelay
public connectPops2,connectPops3,connectPops4,PFSCtotal, PFBCtotal, gapcon,gapconSC,gapconSB,connectPops5,connectPopsML,MFtoGoC_volumetric
objref Nmean,glomdistance,pc,r,GoCPFdistance,MFGoCtotal,MFGCtotal, activeGC,gapcon,r1,vecran,BCinhib,gapconSC,SCinhib,PCSCdelay,list3
objref PFSCtotal, PFBCtotal,PFtotal,Axontotal,tr,trr,gapconSB,SCBCinhib,Rr,Nr,list1,PCidentitiesforBC,PCBCdistance,list2,PCidentitiesforSC,PCSCdistance

external MFstdnorm,NormalMF,numDendGolgi, expPF,FindUniqueGC, ProbMFGC, ProbMFGoC, calprob,calprob1, expPFY,FindGL,FindGC,FindAA
external GoC_nDendML, MFcons,GoCzrange, probAxontoGoC, SCzone, BCzone, MLplug,gseed,BoltzmannPDF,AA_GoC_con
external GoCtoGCsagittal, GoCtoGCmedio, probGoCtoGC, ProbPFtoSC, ProbPFtoBC, saggwidth, nsag,connectacrossbands
external dendz,dendradius, PF_GoC_con, ProbPFtoGoC,TS_pf,TS_mf,TS_goc

external pathPFtoGoCsources, pathPFtoGoCtargets, pathPFtoGoCdistances, pathPFtoGoCsegments
objref FPFtoGoCsources,FPFtoGoCtargets,FPFtoGoCdistances,FPFtoGoCsegments
objref vPFtoGoCsources,vPFtoGoCtargets,vPFtoGoCdistances,vPFtoGoCsegments,mPFtoGoCsegments

external pathAAtoGoCsources, pathAAtoGoCtargets, pathAAtoGoCdistances, pathAAtoGoCsegments
objref FAAtoGoCsources, FAAtoGoCtargets, FAAtoGoCdistances, FAAtoGoCsegments
objref vAAtoGoCsources, vAAtoGoCtargets, vAAtoGoCdistances, vAAtoGoCsegments,mAAtoGoCsegments

external pathGoCtoGoCsources, pathGoCtoGoCtargets, pathGoCtoGoCdistances
external GoC_GoC_inh_con, ProbGoCtoGoCbase, GoCtoGoCdecayrate
objref FGoCtoGoCsources, FGoCtoGoCtargets, FGoCtoGoCdistances
objref vGoCtoGoCsources, vGoCtoGoCtargets, vGoCtoGoCdistances

external pathGoCtoGoCgapsources, pathGoCtoGoCgaptargets, pathGoCtoGoCgapdistances
external vGoCtoGoCgapsources,vGoCtoGoCgaptargets,vGoCtoGoCgapdistances
external GoC_GoC_gap_con
objref FGoCtoGoCgapsources,FGoCtoGoCgaptargets,FGoCtoGoCgapdistances

objref vMFtoGCsources,vMFtoGCtargets,vMFtoGCdistances
external MF_GC_con
objref FMFtoGCsources,FMFtoGCtargets,FMFtoGCdistances



//////////////////////////////////////////////
// MF to GoCs Volumetric connectivity rule //
////////////////////////////////////////////


proc MFtoGoC_volumetric () {local i,j,k,w,x1,y1,z1,count,deldis,mfid,countdend,nDendGolgi,GoC_nDendML,closestglom,closestglom_ind,MossyFiberPopSize,GolgiCellPopSize,distance,TS localobj MossyFiberPop,GolgiCellPop,MFGoCtotal,glomdistance,Nmean,cell,GLmatrix,indGL,GLdistance,MFconn,GLlist

    MossyFiberPop   = $o1    // MF population
    GolgiCellPop    = $o2    // Golgi cell population (Dendrites)
    MFGoCtotal      = $o3
    glomdistance    = $o4
    Nmean           = $o5
    GoCzone         = $6     // cylinder radius (um)
    TS              = $7
    GLmatrix = MossyFiberPop.GLmatrix
    indGL = new Vector()
    GLdistance = new Vector()

    print "MF to GOC Volume"

     for i = 0,GolgiCellPop.GoCcoordinates.nrow-1 { //GoC loop

       x3 = GolgiCellPop.GoCcoordinates.x[i][0]
       y3 = GolgiCellPop.GoCcoordinates.x[i][1]
       z3 = GolgiCellPop.GoCcoordinates.x[i][2]

       MFconn     = new Vector()
       GLlist     = FindGL(GLmatrix,x3,y3,z3,dendradius)
       indGL      = GLlist.object(0)
       GLdistance = GLlist.object(1)

       for j=0,indGL.size()-1 {


	 if (pc.gid_exists(i+GolgiCellPop.startindex)) {
	   cell = pc.gid2cell(i+GolgiCellPop.startindex)
	   mfid = (GLmatrix.x[indGL.x[j]][0])-1+MossyFiberPop.startindex
	   MFconn = cell.MFID.c.indvwhere(cell.MFID,"==",mfid)
	   if (GLdistance.x[indGL.x[j]]<=dendradius && MFconn.size() == 0 &&ProbMFGoC>r.repick()) {
              x1 = MossyFiberPop.MFcoordinates.x[mfid-MossyFiberPop.startindex][0]
              y1 = MossyFiberPop.MFcoordinates.x[mfid-MossyFiberPop.startindex][1]
              z1 = 0
              deldis = sqrt((x3-x1)^2+(y3-y1)^2+(z3-z1)^2)

	     cell.MFID.insrt(0,mfid)
	     cell.MFdel.insrt(0,deldis/TS_mf)
	     cell.GLID.insrt(0,indGL.x[j])
	   }
	 } //if pc
       }
     }
}


proc connectPops1(){local i,j,w,deldis,mfid,probmf,MFcount,x1,x2,y1,x3,y2,z1,z2,z3,closestglom,closestglom_ind,distance,TS,MossyFiberPopSize,GranuleCellPopSize,GolgiCellPopSize,nDendGolgi,BasketCellPopSize,prob localobj MossyFiberPop, GranuleCellPop,GolgiCellPop,StellateCellPop,BasketCellPop, cell,GLlist,indGL,GLdistance,MFconn,GLmatrix,Probvec

    ///////////////////////
    // GL Neurons       //
    /////////////////////

    MossyFiberPop   = $o1    // MF population
    GranuleCellPop  = $o2    // Granule cell population
    GolgiCellPop    = $o3    // Golgi cell population (Dendrites)

    GoCzone         = $4     // cylinder radius (um)
    GCzone          = $5

    TS              = $6       // transmission speed (um/ms)

    MossyFiberPopSize  = MossyFiberPop.MFcoordinates.nrow
    GranuleCellPopSize = GranuleCellPop.GCcoordinates.nrow
    GolgiCellPopSize   = GolgiCellPop.GoCcoordinates.nrow
    nDendGolgi         = numDendGolgi

    ///////////////////////
    // ML Neurons       //
    /////////////////////

    r=new Random(gseed)
    r.uniform(0,1)
    pc = new ParallelContext()

    MFGoCtotal   = new Vector(MossyFiberPopSize)
    MFGCtotal    = new Vector(MossyFiberPopSize)
    PFtotal      = new Vector(GranuleCellPopSize)
    Axontotal    = new Vector(GranuleCellPopSize)
    PFSCtotal    = new Vector(GranuleCellPopSize)
    PFBCtotal    = new Vector(GranuleCellPopSize)
    activeGC     = new Vector()

    if (GoCzone<0) { GoCzone=abs(GoCzone) }
    if (GCzone<0) { GCzone=abs(GCzone) }

    if (MF_GC_con == 1) { // if genearting connections by brep

	FMFtoGCsources   = new File(pathMFtoGCsources)
	FMFtoGCtargets   = new File(pathMFtoGCtargets)
	FMFtoGCdistances = new File(pathMFtoGCdistances)
	FMFtoGCsources.ropen()
	FMFtoGCtargets.ropen()
	FMFtoGCdistances.ropen()
	vMFtoGCsources   = new Vector()
	vMFtoGCtargets   = new Vector()
	vMFtoGCdistances = new Vector()
	vMFtoGCsources.scanf(FMFtoGCsources)
	vMFtoGCtargets.scanf(FMFtoGCtargets)
	vMFtoGCdistances.scanf(FMFtoGCdistances)
	FMFtoGCsources.close()
	FMFtoGCtargets.close()
	FMFtoGCdistances.close()

        pc.barrier()

        if (pc.id == 0) {

	    FMFtoGCsources.unlink()
	    FMFtoGCtargets.unlink()
	    FMFtoGCdistances.unlink()

        }

    }


    ////////////////////////////////////////
    // MF to GCs connectivity rule       //
    ////////////////////////////////////////

    GLlist = new List()
    indGL=new Vector()
    GLdistance = new Vector()
    MFconn=new Vector()
    GLmatrix = MossyFiberPop.GLmatrix
    Probvec = new Vector()
    Probvec.insrt(0,1,1,1,1,0.12,0.18,0.15,0,0) //connectivity probability reduces as number of connections increases


    if  (MF_GC_con == 0) { // if generating connections only by hoc

      for i = 0,GranuleCellPopSize-1 {

	x2 = GranuleCellPop.GCcoordinates.x[i][0]
	y2 = GranuleCellPop.GCcoordinates.x[i][1]
	z2 = GranuleCellPop.GCcoordinates.x[i][2]

	GLlist = FindGL(GLmatrix,x2,y2,z2,GCzone)
	indGL = GLlist.object(0)
	GLdistance = GLlist.object(1)
	MFcount=0

	for j=0,indGL.size()-1 {

	  if (MFcount == 7) {
	    continue
	  }

	  if (pc.gid_exists(i+GranuleCellPop.startindex)) {
	    cell = pc.gid2cell(i+GranuleCellPop.startindex)
	    mfid = (GLmatrix.x[indGL.x[j]][0])-1+MossyFiberPop.startindex
	    MFconn = cell.MFID.c.indvwhere(cell.MFID,"==",mfid)
	    probmf = Probvec.x[MFcount]
	    if (GLdistance.x[indGL.x[j]]<=GCzone && MFconn.size() == 0  && probmf>=r.repick()) {
              x1 = MossyFiberPop.MFcoordinates.x[mfid-MossyFiberPop.startindex][0]
              y1 = MossyFiberPop.MFcoordinates.x[mfid-MossyFiberPop.startindex][1]
              z1 = 0
              deldis = sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)
	      cell.MFID.insrt(0,mfid)
	      cell.MFdel.insrt(0,deldis/TS_mf)
	      cell.GLID.insrt(0,indGL.x[j])
	      MFcount = MFcount+1

	    }
	  }
	}
      }

      print "GCpop to MFpop connected\n"

    } else { // MF_GC_con != 0, connections by brep


	for i=0,vMFtoGCsources.size()-1 {

	    ProbMFtoGoC = Probvec.x[MFcount]

	    if (ProbMFtoGC >= r.repick() && pc.gid_exists(vMFtoGCtargets.x[i]+GranuleCellPop.startindex)) { // connection prob between MF and GC

                for i = 0,MossyFiberPopSize-1 {

	            GLlist = FindGL(GLmatrix,x2,y2,z2,GCzone)
	            indGL  = GLlist.object(0)
	            GLdistance = GLlist.object(1)
	            MFcount=0

	            for j=0,indGL.size()-1 {

	                if (MFcount == 7) {
	                    continue
	                }

	                if (pc.gid_exists(i+GranuleCellPop.startindex)) {

	                    cell = pc.gid2cell(i+GranuleCellPop.startindex)
	                    mfid = (GLmatrix.x[indGL.x[j]][0])-1+MossyFiberPop.startindex
	                    MFconn = cell.MFID.c.indvwhere(cell.MFID,"==",mfid)
	                    probmf = Probvec.x[MFcount]

	                    if (GLdistance.x[indGL.x[j]]<=GCzone && MFconn.size() == 0  && probmf>=r.repick()) {
                                x1 = MossyFiberPop.MFcoordinates.x[GLmatrix.x[indGL.x[j]][0]][0]
                                y1 = MossyFiberPop.MFcoordinates.x[GLmatrix.x[indGL.x[j]][0]][1]
                                z1 = 0
                                deldis = sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)
	                        cell.MFID.insrt(0,mfid)
	                        cell.MFdel.insrt(0,deldis/TS_mf)
	                        cell.GLID.insrt(0,indGL.x[j])
	                        printf("GC id %d connected to mf\n",i)
	                        MFcount = MFcount+1
                            }
                        }
                    }
                }
            }
        }
    }
  MFtoGoC_volumetric (MossyFiberPop,GolgiCellPop,MFGoCtotal,glomdistance,Nmean,GoCzone,TS)


}// end connectPops1


proc connectPops2(){local i,j,w, x1,x3,x2,y1,y2,z1,z2,z3,closestglom,closestglom_ind,distance,TS,MossyFiberPopSize,GranuleCellPopSize,GolgiCellPopSize,nDendGolgi,StellateCellPopSize,BasketCellPopSize,prob localobj MossyFiberPop, GranuleCellPop,GolgiCellPop,StellateCellPop,BasketCellPop,cell,GClist,AAlist,GoCconn,PFconn,AAconn

    MossyFiberPop  = $o1    // MF population
    GranuleCellPop = $o2    // Granule cell population
    GolgiCellPop   = $o3    // Golgi cell population (Dendrites)
    GoCzone        = $4     // cylinder radius (um)
    GCzone         = $5
    TS             = $6     // transmission speed (um/ms)
    MossyFiberPopSize  = MossyFiberPop.nCells
    GranuleCellPopSize = GranuleCellPop.nCells
    GolgiCellPopSize   = GolgiCellPop.nCells
    nDendGolgi         = numDendGolgi
    GoCconn            = new Vector()
    PFconn             = new Vector()
    AAconn             = new Vector()
    r=new Random(gseed)
    r.uniform(0,1)
    pc = new ParallelContext()

    print "connecting PF, AA and GoC\n"
    if (PF_GoC_con == 1) {

	FPFtoGoCsources   = new File(pathPFtoGoCsources)
	FPFtoGoCtargets   = new File(pathPFtoGoCtargets)
	FPFtoGoCdistances = new File(pathPFtoGoCdistances)
	FPFtoGoCsegments  = new File(pathPFtoGoCsegments)
	FPFtoGoCsources.ropen()
	FPFtoGoCtargets.ropen()
	FPFtoGoCdistances.ropen()
	FPFtoGoCsegments.ropen()
	vPFtoGoCsources   = new Vector()
	vPFtoGoCtargets   = new Vector()
	vPFtoGoCdistances = new Vector()
	vPFtoGoCsegments  = new Vector()
        mPFtoGoCsegments  = new Matrix()
	vPFtoGoCsources.scanf(FPFtoGoCsources)
	vPFtoGoCtargets.scanf(FPFtoGoCtargets)
	vPFtoGoCdistances.scanf(FPFtoGoCdistances)
	vPFtoGoCsegments.scanf(FPFtoGoCsegments)
        FPFtoGoCsegments.seek(0)
       	mPFtoGoCsegments.scanf(FPFtoGoCsegments,(vPFtoGoCsegments.size())/2,2)
	FPFtoGoCsources.close()
	FPFtoGoCtargets.close()
	FPFtoGoCdistances.close()
	FPFtoGoCsegments.close()
    	FAAtoGoCsources   = new File(pathAAtoGoCsources)
	FAAtoGoCtargets   = new File(pathAAtoGoCtargets)
	FAAtoGoCdistances = new File(pathAAtoGoCdistances)
	FAAtoGoCsegments  = new File(pathAAtoGoCsegments)
	FAAtoGoCsources.ropen()
	FAAtoGoCtargets.ropen()
	FAAtoGoCdistances.ropen()
	FAAtoGoCsegments.ropen()
	vAAtoGoCsources   = new Vector()
	vAAtoGoCtargets   = new Vector()
	vAAtoGoCdistances = new Vector()
	vAAtoGoCsegments  = new Vector()
        mAAtoGoCsegments  = new Matrix()
	vAAtoGoCsources.scanf(FAAtoGoCsources)
	vAAtoGoCtargets.scanf(FAAtoGoCtargets)
	vAAtoGoCdistances.scanf(FAAtoGoCdistances)
	vAAtoGoCsegments.scanf(FAAtoGoCsegments)
        FAAtoGoCsegments.seek(0)
        mAAtoGoCsegments.scanf(FAAtoGoCsegments,(vAAtoGoCsegments.size())/2,2)
	FAAtoGoCsources.close()
	FAAtoGoCtargets.close()
	FAAtoGoCdistances.close()
	FAAtoGoCsegments.close()

        print "deleting files\n"

	    FPFtoGoCsources.unlink()
	    FPFtoGoCtargets.unlink()
	    FPFtoGoCdistances.unlink()
	    FPFtoGoCsegments.unlink()
	    FAAtoGoCsources.unlink()
	    FAAtoGoCtargets.unlink()
	    FAAtoGoCdistances.unlink()
	    FAAtoGoCsegments.unlink()

    }

    if (GoC_GoC_inh_con == 1) {
	FGoCtoGoCsources = new File(pathGoCtoGoCsources)
	FGoCtoGoCtargets =  new File(pathGoCtoGoCtargets)
	FGoCtoGoCdistances =  new File(pathGoCtoGoCdistances)
	FGoCtoGoCsources.ropen()
	FGoCtoGoCtargets.ropen()
	FGoCtoGoCdistances.ropen()
	vGoCtoGoCsources = new Vector()
	vGoCtoGoCtargets = new Vector()
	vGoCtoGoCdistances = new Vector()
	vGoCtoGoCsources.scanf(FGoCtoGoCsources)
	vGoCtoGoCtargets.scanf(FGoCtoGoCtargets)
	vGoCtoGoCdistances.scanf(FGoCtoGoCdistances)
	FGoCtoGoCsources.close()
	FGoCtoGoCtargets.close()
	FGoCtoGoCdistances.close()
    }

    if (GoC_GoC_gap_con == 1) {

	FGoCtoGoCgapsources = new File(pathGoCtoGoCgapsources)
	FGoCtoGoCgaptargets =  new File(pathGoCtoGoCgaptargets)
	FGoCtoGoCgapdistances =  new File(pathGoCtoGoCgapdistances)
	FGoCtoGoCgapsources.ropen()
	FGoCtoGoCgaptargets.ropen()
	FGoCtoGoCgapdistances.ropen()
	vGoCtoGoCgapsources = new Vector()
	vGoCtoGoCgaptargets = new Vector()
	vGoCtoGoCgapdistances = new Vector()
	vGoCtoGoCgapsources.scanf(FGoCtoGoCgapsources)
	vGoCtoGoCgaptargets.scanf(FGoCtoGoCgaptargets)
	vGoCtoGoCgapdistances.scanf(FGoCtoGoCgapdistances)
	FGoCtoGoCgapsources.close()
	FGoCtoGoCgaptargets.close()
	FGoCtoGoCgapdistances.close()
    }

    if (MLplug==1) { // This part is reserved for future development

	    StellateCellPop     = $o7    // Stellate population
	    BasketCellPop       = $o8    // Basket population
	    StellateCellPopSize = StellateCellPop.nCells
	    BasketCellPopSize   = BasketCellPop.nCells

    }

    MFGoCtotal = new Vector(MossyFiberPopSize)
    MFGCtotal  = new Vector(MossyFiberPopSize)
    PFtotal    = new Vector(GranuleCellPopSize)
    Axontotal  = new Vector(GranuleCellPopSize)
    PFSCtotal  = new Vector(GranuleCellPopSize)
    PFBCtotal  = new Vector(GranuleCellPopSize)
    activeGC   = new Vector()

    if (GoCzone<0) { GoCzone=abs(GoCzone) }
    if (GCzone<0) { GCzone=abs(GCzone) }
    print "Inhibitory connection between GoC and Goc\n"

    /////////////////////////////////////////////////
    // Inhibitory connections between GoC and GoC
    ////////////////////////////////////////////////
    if (GoC_GoC_inh_con == 1) { // by brep

	    for i=0,vGoCtoGoCsources.size()-1 {

                xsource = GolgiCellPop.GoCcoordinates.x[vGoCtoGoCsources.x[i]][0]
                ysource = GolgiCellPop.GoCcoordinates.x[vGoCtoGoCsources.x[i]][1]
                zsource = GolgiCellPop.GoCcoordinates.x[vGoCtoGoCsources.x[i]][2]

                xtarget = GolgiCellPop.GoCcoordinates.x[vGoCtoGoCtargets.x[i]][0]
                ytarget = GolgiCellPop.GoCcoordinates.x[vGoCtoGoCtargets.x[i]][1]
                ztarget = GolgiCellPop.GoCcoordinates.x[vGoCtoGoCtargets.x[i]][2]
                dis     = sqrt((xsource-xtarget)^2+(ysource-ytarget)^2+(zsource-ztarget)^2)

		//ProbGoCtoGoC = ProbGoCtoGoCbase - GoCtoGoCdecayrate * vGoCtoGoCdistances.x[i]
                ProbGoCtoGoC = BoltzmannPDF(dis, 171,-1945,267,84) / 100

		if (ProbGoCtoGoC >= r.repick() && pc.gid_exists(vGoCtoGoCtargets.x[i]+GolgiCellPop.startindex)) { // inh connection prob between GoC and GoC

		    cell = pc.gid2cell(vGoCtoGoCtargets.x[i]+GolgiCellPop.startindex)
                    GoCconn = cell.GoCID.c.indvwhere(cell.GoCID,"==",vGoCtoGoCsources.x[i]+GolgiCellPop.startindex)
                    if (GoCconn.size == 0) {
		    cell.GoCID.append(vGoCtoGoCsources.x[i]+GolgiCellPop.startindex)
		    cell.GoCdel.append(vGoCtoGoCdistances.x[i]/TS_goc)
                    }
		}
	    }
    }

    ////////////////////////////////////////////////////
    // GoCs to GCs and GCs to GoCs connectivity rules //
    ///////////////////////////////////////////////////

    if (PF_GoC_con == 1) {

	    for i=0,vPFtoGoCsources.size()-1 {

		if (ProbPFtoGoC >= r.repick() && pc.gid_exists(vPFtoGoCtargets.x[i]+GolgiCellPop.startindex)) { // connection prob between PF and GoC

		    cell = pc.gid2cell(vPFtoGoCtargets.x[i]+GolgiCellPop.startindex)
                    PFconn = cell.PFID.c.indvwhere(cell.PFID,"==",vPFtoGoCsources.x[i]+GranuleCellPop.startindex)
                    if (PFconn.size() == 0 ) {
                    cell.PFID.append(vPFtoGoCsources.x[i]+GranuleCellPop.startindex)
                    cell.PFdel.append((vPFtoGoCdistances.x[i])/TS_pf)
                    cell.PFseg.append(mPFtoGoCsegments.x[i][0])
                    cell.PFdend.append(mPFtoGoCsegments.x[i][1])

                }
	    }
	}
    }


    if (AA_GoC_con == 1) {    //connections by brep

	    for i=0,vAAtoGoCsources.size()-1 {

		if (probAxontoGoC >= r.repick() && pc.gid_exists(vAAtoGoCtargets.x[i]+GolgiCellPop.startindex)) {

		    cell = pc.gid2cell(vAAtoGoCtargets.x[i]+GolgiCellPop.startindex)
                    AAconn = cell.AxonID.c.indvwhere(cell.AxonID,"==",vAAtoGoCsources.x[i]+GranuleCellPop.startindex)
                    if (AAconn.size() == 0) {
                    cell.AxonID.append(vAAtoGoCsources.x[i]+GranuleCellPop.startindex)
                    cell.Axondel.append((vAAtoGoCdistances.x[i])/TS_pf)
                    cell.AAseg.append(mAAtoGoCsegments.x[i][0])
                    cell.AAdend.append(mAAtoGoCsegments.x[i][1])

                }
	    }
	}
    }

    GClist = new Vector()
    AAlist=  new Vector()
    GoCGCcount=0

    print "Inhibitory connection between GoC and Gc\n"

    for i = 0,GolgiCellPopSize-1 {

        // Inhibitory connections between GoC and GC

        // GoC coordinates
        x3 = GolgiCellPop.GoCcoordinates.x[i][0]
        y3 = GolgiCellPop.GoCcoordinates.x[i][1]
        z3 = GolgiCellPop.GoCcoordinates.x[i][2]

	GClist = FindGC(GranuleCellPop.GCcoordinates,x3,y3,GoCtoGCmedio/2,GoCtoGCsagittal/2)

         for j=0, GClist.size()-1 {

	   x2   = GranuleCellPop.GCcoordinates.x[GClist.x[j]][0]
	   y2   = GranuleCellPop.GCcoordinates.x[GClist.x[j]][1]
	   z2   = GranuleCellPop.GCcoordinates.x[GClist.x[j]][2]
	   deltax=abs(x3-x2)
	   deltay=abs(y3-y2)

	   if(deltax<=GoCtoGCmedio/2&&deltay<=GoCtoGCsagittal/2&&probGoCtoGC>=r.repick()&&pc.gid_exists(GClist.x[j]+GranuleCellPop.startindex)){
	     cell = pc.gid2cell(GClist.x[j]+GranuleCellPop.startindex)
	     cell.GoCID.append(i+GolgiCellPop.startindex)
	     distanceGoCGC = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)+(z3-z2)*(z3-z2))
	     cell.GoCdel.append(distanceGoCGC/TS_goc)
	   }
	 }


         //print "GC ascending axon-GOC connection\n"

        if (AA_GoC_con == 0) { //if generating conn by hoc
        AAlist = FindAA(GranuleCellPop.GCcoordinates,x3,y3,dendradius)

        for j=0,AAlist.size()-1 {

	  x2   = GranuleCellPop.GCcoordinates.x[AAlist.x[j]][0]
	  y2   = GranuleCellPop.GCcoordinates.x[AAlist.x[j]][1]
	  z2   = GranuleCellPop.GCcoordinates.x[AAlist.x[j]][2]
	  deltazaxon= z2-z3  // if <0, GC bellow GoC
	  deltazML = abs(z2-z3)

	  if(deltazaxon<0 && probAxontoGoC>=r.repick()&&pc.gid_exists(i+GolgiCellPop.startindex)){

            cell = pc.gid2cell(i+GolgiCellPop.startindex)
            cell.AxonID.append(AAlist.x[j]+GranuleCellPop.startindex)
            cell.Axondel.append(deltazML/TS_pf)
            cell.AMPAGLid.append(k)
	  }
        }
    }

    } // end GoC loop

   /* if (MLplug==1){ // This part is reserved for future development

      countPFSC=0
      countPFBC=0

	for i = 0,GranuleCellPopSize-1 {

	  if (pc.id == 0) {

	    for j = 0,StellateCellPopSize-1 { // SC cells loop

	      x4 = GranuleCellPop.Tcoordinates.x[i][0]
	      y4 = GranuleCellPop.Tcoordinates.x[i][1]
	      z4 = GranuleCellPop.Tcoordinates.x[i][2]

	      x5 = StellateCellPop.SCcoordinates.x[j][0]
	      y5 = StellateCellPop.SCcoordinates.x[j][1]
	      z5 = StellateCellPop.SCcoordinates.x[j][2]

	      distancePFSC = sqrt((y4-y5)*(y4-y5) + (z4-z5)*(z4-z5))

	      if(distancePFSC<=SCzone&&ProbPFtoSC>=r.repick()){
		countPFSC= countPFSC+1
	      }

	      if(distancePFSC<=SCzone&&ProbPFtoSC>=r.repick()&&pc.gid_exists(j+StellateCellPop.startindex)){

		cell = pc.gid2cell(j+StellateCellPop.startindex)
		cell.PFID.append(i+GranuleCellPop.startindex)
		cell.PFdel.append(distancePFSC/TS)
	      }
	    }

	    ///////////////////////////////////
	    // PFs to BC connectivity rule   //
	    ///////////////////////////////////

	    for j = 0,BasketCellPopSize-1 { // BC cells loop

	      x6 = BasketCellPop.BCcoordinates.x[j][0]
	      y6 = BasketCellPop.BCcoordinates.x[j][1]
	      z6 = BasketCellPop.BCcoordinates.x[j][2]

	      distancePFBC = sqrt((y4-y6)*(y4-y6) + (z4-z6)*(z4-z6))
	      deltazPF = z6-z4  // Semi-circular connectivity

	      if(distancePFBC<=BCzone&&ProbPFtoBC>=r.repick()&&deltazPF>0){
		countPFBC= countPFBC+1
	      }

	      if(distancePFBC<=BCzone&&ProbPFtoBC>=r.repick()&&deltazPF>0&&pc.gid_exists(j+BasketCellPop.startindex)){

		cell = pc.gid2cell(j+BasketCellPop.startindex)

		cell.PFID.append(i+GranuleCellPop.startindex)
		cell.PFdel.append(distancePFBC/TS)
	      }
	    }

	  } // end pc.id == 0


	} // end GC loop


    } // end ML connections */

} // end proc



// Molecular layer connectivity: Gap Junctions and inhibitory connections
proc connectPops3(){local i,j,w, x7,x8,x9,x10,x11,x12,y7,y8,y9,y10,y11,y12,z7,z8,z9,z10,z11,z12,distance,TS,MossyFiberPopSize,GranuleCellPopSize,GolgiCellPopSize,nDendGolgi,StellateCellPopSize,BasketCellPopSize,prob localobj MossyFiberPop, GranuleCellPop,GolgiCellPop,StellateCellPop,BasketCellPop,cell

    MossyFiberPop  = $o1    // MF population
    GranuleCellPop = $o2    // Granule cell population
    GolgiCellPop   = $o3    // Golgi cell population (Dendrites)
    GoCzone        = $4     // cylinder radius (um)
    GCzone         = $5
    TS             = $6       // transmission speed (um/ms)

    MossyFiberPopSize  = MossyFiberPop.nCells
    GranuleCellPopSize = GranuleCellPop.nCells
    GolgiCellPopSize   = GolgiCellPop.nCells
    nDendGolgi         = numDendGolgi
    numgapsSC          = 0

    ///////////////////////
    // ML Neurons       //
    /////////////////////



    StellateCellPop  = $o7    // Stellate population
    BasketCellPop    = $o8    // Basket population
    StellateCellPopSize = StellateCellPop.nCells
    BasketCellPopSize   = BasketCellPop.nCells

    /////////////// BC-BC gap connectivity rule /////////////

    numgaps = 0
    nSC = 0
    nBC = 0
    tr = new Random(gseed)
    tr.uniform(0,1)

    // connectivity matrix

    gapconSC  = new Matrix(StellateCellPopSize,StellateCellPopSize)
    SCinhib   = new Matrix(StellateCellPopSize,StellateCellPopSize)
    gapcon    = new Matrix(BasketCellPopSize,BasketCellPopSize)
    BCinhib   = new Matrix(BasketCellPopSize,BasketCellPopSize)



    if (pc.id == 0) {

    // Gap connectivity rule for BC
    for  i = 0, BasketCellPopSize-1 {


        for  j = 0, BasketCellPopSize-1{

        if (i==j) {
            continue
        }

        if (gapcon.x[j][i] == 1) {

            continue
        }

        x7 = BasketCellPop.BCcoordinates.x[j][0]
        y7 = BasketCellPop.BCcoordinates.x[j][1]
        z7 = BasketCellPop.BCcoordinates.x[j][2]

        x8 = BasketCellPop.BCcoordinates.x[i][0]
        y8 = BasketCellPop.BCcoordinates.x[i][1]
        z8 = BasketCellPop.BCcoordinates.x[i][2]


        disBC = sqrt((y8-y7)*(y8-y7)+(z8-z7)*(z8-z7))
        probBC =  calprob1(disBC) //((-0.5/90)*dis)+0.5

        if (connectacrossbands == 1) {

            if (disBC<=80 && tr.repick()<=probBC) {

                cell = pc.gid2cell(i+BasketCellPop.startindex)
                cell.gapid.append(j+BasketCellPop.startindex)
                numgaps = numgaps +1
                gapcon.x[i][j] = 1

            }
            } else {


            for (k=1;k<=nsag;k+=1) {
                if (k==1) {

                if (x7<= saggwidth && x8<= saggwidth) {
                    //if (abs(x8-x7)<=dendspread && tr.repick()<=probgapforBC ) {


                    if (disBC<=80 && tr.repick()<=probBC) {
                    cell = pc.gid2cell(i+BasketCellPop.startindex)
                    cell.gapid.append(j+BasketCellPop.startindex)
                    numgaps = numgaps +1
                    gapcon.x[i][j] = 1

                    }
                }
                } else { //if k>1

                if ((x7<= saggwidth*k && x7> saggwidth*(k-1))  && (x8<= saggwidth*k && x8> saggwidth*(k-1))){
                    //if (abs(x8-x7)<=dendspread && r.repick()<=probgapforBC ) {
                    if (disBC<=80 && tr.repick()<=probBC) {

                    cell = pc.gid2cell(i+BasketCellPop.startindex)
                    cell.gapid.append(j+BasketCellPop.startindex)
                    numgaps = numgaps +1
                    gapcon.x[i][j] = 1
                    }
                }

                }//else
            } //for k

            }// connectacrossbands


        }
        }


        ///////// BC-BC GABA rule ////////

        for  i = 0, BasketCellPopSize-1 {

        for  j = 0, BasketCellPopSize-1 {


        x7 = BasketCellPop.BCcoordinates.x[j][0]
        y7 = BasketCellPop.BCcoordinates.x[j][1]
        z7 = BasketCellPop.BCcoordinates.x[j][2]

        x8 = BasketCellPop.BCcoordinates.x[i][0]
        y8 = BasketCellPop.BCcoordinates.x[i][1]
        z8 = BasketCellPop.BCcoordinates.x[i][2]


        if (i==j) {
            continue
        }

        if (BCinhib.x[j][i] == 1) {
            disBC1 = sqrt((y8-y7)*(y8-y7)+(z8-z7)*(z8-z7))
            probBC1 = 0.08
            if (disBC1<=80 && r.repick()<=probBC1 ) {
            cell = pc.gid2cell(i+BasketCellPop.startindex)
            cell.BCID.append(j+BasketCellPop.startindex) // ith cell receives connection from jth cell
            cell.BCdelay.append(disBC1/TS)
            nBC = nBC +1
            BCinhib.x[i][j] = 1
            }

        } else {

            disBC1 = sqrt((y8-y7)*(y8-y7)+(z8-z7)*(z8-z7))
            probBC1 = calprob(int(disBC1))

            if (disBC1<=80 && r.repick()<=probBC1 ) {
            cell = pc.gid2cell(i+BasketCellPop.startindex)
            cell.BCID.append(j+BasketCellPop.startindex) // ith cell receives connection from jth cell
            cell.BCdelay.append(disBC1/TS)
            nBC = nBC +1
            BCinhib.x[i][j] = 1
            }
        }


        }
    }


    ///////// SC-SC gap connectivity rule /////////

    for  i = 0, StellateCellPopSize-1 {


        for  j = 0, StellateCellPopSize-1 {

        if (i==j) {
            continue
        }

        if (gapconSC.x[j][i] == 1) {

            continue
        }

        x9 = StellateCellPop.SCcoordinates.x[j][0]
        y9 = StellateCellPop.SCcoordinates.x[j][1]
        z9 = StellateCellPop.SCcoordinates.x[j][2]

        x10 = StellateCellPop.SCcoordinates.x[i][0]
        y10 = StellateCellPop.SCcoordinates.x[i][1]
        z10 = StellateCellPop.SCcoordinates.x[i][2]


        disSC = sqrt((y10-y9)*(y10-y9)+(z10-z9)*(z10-z9))
        probSC = calprob1(disSC)

        if (connectacrossbands == 1) {
            if (disSC<=80 && tr.repick()<=probSC) {

            cell = pc.gid2cell(i+StellateCellPop.startindex)
            cell.gapid.append(j+StellateCellPop.startindex)
            numgapsSC = numgapsSC +1
            gapconSC.x[i][j] = 1

            }
        } else {

            for (k=1;k<=nsag;k+=1) {

            if (k==1) {

                if (x9<= saggwidth && x10<= saggwidth) {

                //if (abs(x8-x7)<=dendspread && tr.repick()<=probgapforBC ) {

                if (disSC<=80 && tr.repick()<=probSC) {

                    cell = pc.gid2cell(i+StellateCellPop.startindex)
                    cell.gapid.append(j+StellateCellPop.startindex)
                    numgapsSC = numgapsSC +1
                    gapconSC.x[i][j] = 1

                }
                }
            }else { //if k>1

                if ((x9<= saggwidth*k && x9> saggwidth*(k-1))  && (x10<= saggwidth*k && x10> saggwidth*(k-1))){

                //if (abs(x8-x7)<=dendspread && r.repick()<=probgapforBC ) {
                if (disSC<=80 && tr.repick()<=probSC) {

                    cell = pc.gid2cell(i+StellateCellPop.startindex)
                    cell.gapid.append(j+StellateCellPop.startindex)
                    numgapsSC = numgapsSC +1
                    gapconSC.x[i][j] = 1
                }
                }
            } // else
            } // for
        } // connectacrossbands
        }
    }

    print "numgapsSC"
    print numgapsSC


    ///SC-SC GABA rule

    for  i = 0, StellateCellPopSize-1{

        for  j = 0, StellateCellPopSize-1{

        x9 = StellateCellPop.SCcoordinates.x[j][0]
        y9 = StellateCellPop.SCcoordinates.x[j][1]
        z9 = StellateCellPop.SCcoordinates.x[j][2]

        x10 = StellateCellPop.SCcoordinates.x[i][0]
        y10 = StellateCellPop.SCcoordinates.x[i][1]
        z10 = StellateCellPop.SCcoordinates.x[i][2]


        if (i==j) {
            continue
        }

        if (SCinhib.x[j][i] == 1) {

            disSC1 = sqrt((y10-y9)*(y10-y9)+(z10-z9)*(z10-z9))
            probSC1 = 0.08

            if (disSC1<=80 && r.repick()<=probSC1 ) {
            cell = pc.gid2cell(i+StellateCellPop.startindex)
            cell.SCID.append(j+StellateCellPop.startindex) // ith cell receives connection from jth cell
            cell.SCdelay.append(disSC1/TS)
            nSC = nSC+1
            SCinhib.x[i][j] = 1
        }

        } else {


            disSC1 = sqrt((y10-y9)*(y10-y9)+(z10-z9)*(z10-z9))
            probSC1 = calprob(int(disSC1))

            if (disSC1<=80 && r.repick()<=probSC1 ) {
            cell = pc.gid2cell(i+StellateCellPop.startindex)
            cell.SCID.append(j+StellateCellPop.startindex) // ith cell receives connection from jth cell
            cell.SCdelay.append(disSC1/TS)
            nSC = nSC+1
            SCinhib.x[i][j] = 1
            }
        }


        }
    }



    }// pc.id
}//end proc


proc connectPops4(){local i,j,w, x7,x8,x9,x10,x11,x12,y7,y8,y9,y10,y11,y12,z7,z8,z9,z10,z11,z12,distance,TS,MossyFiberPopSize,GranuleCellPopSize,GolgiCellPopSize,nDendGolgi,StellateCellPopSize,BasketCellPopSize,PurkinjeCellPopSize,prob localobj MossyFiberPop, GranuleCellPop,GolgiCellPop,StellateCellPop,BasketCellPop,PurkinjeCellPop,cell,er,PCSCdelay

    MossyFiberPop  = $o1    // MF population
    GranuleCellPop = $o2    // Granule cell population
    GolgiCellPop   = $o3    // Golgi cell population (Dendrites)
    GoCzone        = $4     // cylinder radius (um)
    GCzone         = $5
    TS             = $6      // transmission speed (um/ms)

    MossyFiberPopSize  = MossyFiberPop.nCells
    GranuleCellPopSize = GranuleCellPop.nCells
    GolgiCellPopSize   = GolgiCellPop.nCells
    nDendGolgi         = numDendGolgi
    //numgapsSC = 0

    ///////////////////////
    // ML Neurons       //
    /////////////////////


    StellateCellPop  = $o7    // Stellate population
    BasketCellPop    = $o8    // Basket population
    StellateCellPopSize  = StellateCellPop.nCells
    BasketCellPopSize    = BasketCellPop.nCells

    /////////////// BC-SC gap connectivity rule /////////////


    nSCBC = 0
    numgapsSCBC = 0
    gapconSB  = new Matrix(StellateCellPopSize,BasketCellPopSize)
    SCBCinhib = new Matrix(StellateCellPopSize,BasketCellPopSize)
    tr = new Random(gseed)
    tr.uniform(0,1)
    Rr = new Random(gseed)

    // connectivity matrix

    if (pc.id == 0) {
    ////// BC-SC Gap Connectivity //

    for  i = 0, StellateCellPopSize-1{

        for  j = 0, BasketCellPopSize-1{

            x11 = BasketCellPop.BCcoordinates.x[j][0]
            y11 = BasketCellPop.BCcoordinates.x[j][1]
            z11 = BasketCellPop.BCcoordinates.x[j][2]

            x12 = StellateCellPop.SCcoordinates.x[i][0]
            y12 = StellateCellPop.SCcoordinates.x[i][1]
            z12 = StellateCellPop.SCcoordinates.x[i][2]


            disSCBC = sqrt((y12-y11)*(y12-y11)+(z12-z11)*(z12-z11))
            probSCBC = calprob1(probSCBC)

            if (connectacrossbands == 1) {

		if (disSCBC<=80 && tr.repick()<=probSCBC) {

		    cell = pc.gid2cell(j+BasketCellPop.startindex)
		    cell.gapid.append(i+StellateCellPop.startindex)
		    numgapsSCBC = numgapsSCBC +1
		    gapconSB.x[i][j] = 1

		}
            } else {

		for (k=1;k<=nsag;k+=1) {

		    if ((x11<= saggwidth*k && x11> saggwidth*(k-1))  && (x12<= saggwidth*k && x12> saggwidth*(k-1))){
			//if (abs(x8-x7)<=dendspread && r.repick()<=probgapforBC ) {
			if (disSCBC<=80 && tr.repick()<=probSCBC) {

			    cell = pc.gid2cell(j+BasketCellPop.startindex)
			    cell.gapid.append(i+StellateCellPop.startindex)
			    numgapsSCBC = numgapsSCBC +1
			    gapconSB.x[i][j] = 1
			}
		    }

		} //for

            } // if connectacrossbands

	}
    }


    // SC-BC gaba rule
    t1 = 0
    t2 = 0
    for  i = 0, StellateCellPopSize-1 {

        for  j = 0, BasketCellPopSize-1 {

        x11 = BasketCellPop.BCcoordinates.x[j][0]
        y11 = BasketCellPop.BCcoordinates.x[j][1]
        z11 = BasketCellPop.BCcoordinates.x[j][2]

        x12 = StellateCellPop.SCcoordinates.x[i][0]
        y12 = StellateCellPop.SCcoordinates.x[i][1]
        z12 = StellateCellPop.SCcoordinates.x[i][2]

        disSCBC1 = sqrt((y12-y11)*(y12-y11)+(z12-z11)*(z12-z11))//sagital distance

        probSCBC1 = calprob(int(disSCBC1))

        if (disSCBC1<=80 && r.repick()<=probSCBC1 ) {

            if (Rr.uniform(0,1)<=0.667 ) {

            cell = pc.gid2cell(i+StellateCellPop.startindex)
            cell.SCID.append(j+BasketCellPop.startindex) // ith cell receives connection from jth cell
            cell.SCdelay.append(disSCBC1/TS)
            nSCBC = nSCBC+1
            SCBCinhib.x[i][j] = 1
            flag = 1
            t1 = t1+1

            } else {

            cell = pc.gid2cell(j+BasketCellPop.startindex)
            cell.BCID.append(i+StellateCellPop.startindex) // ith cell receives connection from jth cell
            cell.BCdelay.append(disSCBC1/TS)
            nSCBC = nSCBC+1
            SCBCinhib.x[i][j] = 1
            flag = 0
            t2 = t2+1
            }

        }

        if (SCBCinhib.x[i][j] == 1) { // reciprocal connection

            disSCBC1 = sqrt((y12-y11)*(y12-y11)+(z12-z11)*(z12-z11))

            probreciprocal = 0.08 //from sarah's poster

            if (disSCBC1<=80 && r.repick()<=probreciprocal ) {
            if (flag ==1) {

                cell = pc.gid2cell(j+BasketCellPop.startindex)
                cell.BCID.append(i+StellateCellPop.startindex) // ith cell receives connection from jth cell
                cell.BCdelay.append(disSCBC1/TS)
                nSCBC = nSCBC+1

            } else {

                cell = pc.gid2cell(j+StellateCellPop.startindex)
                cell.SCID.append(i+BasketCellPop.startindex) // ith cell receives connection from jth cell
                cell.SCdelay.append(disSCBC1/TS)
                nSCBC = nSCBC+1

            }
            }

        }

        }
    }

    }// pc.id


    Nr = new Random(gseed)
    Nr.normal(91,4489)
    nPCBC = 0

    PurkinjeCellPop = $o9
    PurkinjeCellPopSize = PurkinjeCellPop.nrow
    PurkinjeCellPopStartindex = $10

    PCidentitiesforBC = new List()
    PCBCdistance = new List()

    for i=0, BasketCellPopSize-1 {
    list1 = new Vector()
    list2 = new Vector()
    PCidentitiesforBC.append(list1)
    PCBCdistance.append(list2)
    }


    for  i = 0, PurkinjeCellPopSize-1 {
    perPC = 0
    for  j = 0, BasketCellPopSize-1 {

        x13 = PurkinjeCellPop.x[i][0]
        y13 = PurkinjeCellPop.x[i][1]
        z13 = PurkinjeCellPop.x[i][2]

        x14 = BasketCellPop.BCcoordinates.x[j][0]
        y14 = BasketCellPop.BCcoordinates.x[j][1]
        z14 = BasketCellPop.BCcoordinates.x[j][2]

        disPCBC = sqrt((y14-y13)*(y14-y13)+(z14-z13)*(z14-z13))

        deltay = abs(y13-y14)

        pr = (-0.333*disPCBC)+77.97
        if (disPCBC <=190 && tr.repick()<=pr && deltay<=Nr.repick() ) {

        PCidentitiesforBC.object(j).append(i+PurkinjeCellPopStartindex)

        PCBCdistance.object(j).append(abs(y14-y13))
        in = pc.gid_exists(i+PurkinjeCellPopStartindex)
        nPCBC = nPCBC+1

        if (pc.gid_exists(i+PurkinjeCellPopStartindex)) {
            pc.gid2cell(i+PurkinjeCellPopStartindex).BCID.append(j+BasketCellPop.startindex)
            pc.gid2cell(i+PurkinjeCellPopStartindex).BCdelay.append(disPCBC)

            perPC =perPC+1

        }
    }


}
}


    if (pc.id == 0) {
        ////// BC-SC Gap Connectivity //

        for  i = 0, BasketCellPopSize-1 {
            pc.gid2cell(i+BasketCellPop.startindex).PCdelay = PCBCdistance.object(i)
            pc.gid2cell(i+BasketCellPop.startindex).PCID = PCidentitiesforBC.object(i)
        }
    }


    ////////////// SC to PC connectivity rule //////////////

    nPCSC = 0
    PCidentitiesforSC = new List()
    PCSCdistance = new List()
    PCSCdelay = new List()

    for i=0, StellateCellPopSize-1 {
    list1 = new Vector()
    list2 = new Vector()
    list3 = new Vector()
    PCidentitiesforSC.append(list1)
    PCSCdistance.append(list2)
    PCSCdelay.append(list3)
    }


    for  i = 0, PurkinjeCellPopSize-1 {

    perPC = 0

    for  j = 0, StellateCellPopSize-1 {

        x13 = PurkinjeCellPop.x[i][0]
        y13 = PurkinjeCellPop.x[i][1]
        z13 = PurkinjeCellPop.x[i][2]

        x14 = StellateCellPop.SCcoordinates.x[j][0]
        y14 = StellateCellPop.SCcoordinates.x[j][1]
        z14 = StellateCellPop.SCcoordinates.x[j][2]

        disPCSC = sqrt((y14-y13)*(y14-y13)+(z14-z13)*(z14-z13))

        deltay = abs(y13-y14)
        pr = (-0.333*disPCBC)+77.97
        if (disPCSC <=190 && tr.repick()<=pr && deltay<=Nr.repick() ) {
        PCidentitiesforSC.object(j).append(i+PurkinjeCellPopStartindex)

        PCSCdistance.object(j).append(abs(y14-y13))
        PCSCdelay.object(j).append(disPCSC)
        nPCSC = nPCSC+1

        if (pc.gid_exists(i+PurkinjeCellPopStartindex)) {
            pc.gid2cell(i+PurkinjeCellPopStartindex).SCID.append(j+StellateCellPop.startindex)
            pc.gid2cell(i+PurkinjeCellPopStartindex).SCdelay.append(disPCSC)

        }
        }
    }
    }

    if (pc.id == 0) {
    ////// BC-SC Gap Connectivity //

    for  i = 0, StellateCellPopSize-1{
        pc.gid2cell(i+StellateCellPop.startindex).PCdelay = PCSCdistance.object(i)
        pc.gid2cell(i+StellateCellPop.startindex).PCID = PCidentitiesforSC.object(i)
    }
    }



}//end proc



endtemplate enPassage