// Created by FF (2018)
// Proc here are used to connect all cells in network
objref InternalsynList,InternalncList,rc_E2E,rc_I2I,rc_I2E,rc_E2I
InternalsynList = new List()
InternalncList = new List()
//rdm = new Random()

maxdelay=2.425 ////define maximum delay,ms
mindelay=0.8 ////define minmum delay,ms
maxdis=2.425  /// mm sqrt((1.4)^2+(1.4)^2+(1.4)^2)

proc ConnectInternal() { local i,j,cellgid,inputid,synid,thr,wgt,del localobj vx
      for i = 0,TotalCellNum-1 {
      cellgid=i
      if(!pc.gid_exists(cellgid)) { continue }
        ind_start=1+3*cellgid
        ind_stop=2+3*cellgid     
        vx=syn_connect.at(syn_ind.x[ind_start],syn_ind.x[ind_stop])  ///to get all pre syns ID
         for jj=0,vx.size()-1   {
              inputid=vx.x[jj]
            if (cellgid<CellNum_p&&inputid<CellNum_p) {
               synid = 1    ///for P2P connections
               rc_E2E = new Random(cellgid*TotalCellNum+inputid+1)
               wgt = rc_E2E.lognormal(2,1)
               del_fluc = rc_E2E.uniform(-0.1,0.1)                              
            } else if (cellgid<CellNum_p&&inputid>=CellNum_p) {
               synid = 3      ///for I2P connections 
               rc_I2E = new Random(cellgid*TotalCellNum+inputid+1)
               wgt = rc_I2E.lognormal(12,2)
               del_fluc = rc_I2E.uniform(-0.1,0.1)                              
             } else if (cellgid>=CellNum_p&&inputid<CellNum_p) {
               synid = 2      ///for P2I connections 
               rc_E2I = new Random(cellgid*TotalCellNum+inputid+1)
               wgt = rc_E2I.lognormal(7,2)
               del_fluc = rc_E2I.uniform(-0.1,0.1)                                 
              } else  {
               synid = 4   
               rc_I2I = new Random(cellgid*TotalCellNum+inputid+1)
               wgt = rc_I2I.lognormal(20,10)
               del_fluc = rc_I2I.uniform(-0.1,0.1)
             }
             dis=sqrt((Location.x[inputid][0]-Location.x[cellgid][0])^2+(Location.x[inputid][1]-Location.x[cellgid][1])^2+(Location.x[inputid][2]-Location.x[cellgid][2])^2)
			 del=(dis/maxdis)*maxdelay+mindelay+del_fluc+dt   /// also see delay equation in ms.
            ConnectTwoCells(inputid,cellgid,synid,wgt,del,InternalsynList,InternalncList)

}
}
	 {pc.barrier()}
     saveM.close()
}