load_file("nrngui.hoc")
load_file("F_1.hoc")
load_file("F_1.hoc")
load_file("F_2.hoc") 
load_file("F_3.hoc") 
load_file("F_4.hoc")
load_file("F_5.hoc")
load_file("F_6.hoc")
load_file("F_7.hoc") 
load_file("F_8.hoc") 
load_file("F_9.hoc")
 
//Setting stop time 
tstop=2000

//Setting Vth 
Vth=-20 
Vm=-60
VI=-85

//Number of neurons
N=100

//Synaptic time constant
t1=2
t2=3

//Synaptic conductance 
Gsyn=1.3
GsynI=1.3
GsynR=1



objref delay, weight ,Vec_dey,Vec_wei
 
delay = new Random()
 
weight= new Random()
delay.normal(1300, 300)
Vec_dey = new Vector(N)
Vec_dey.setrand(delay)
weight.normal(0.5,0.05)
Vec_wei = new Vector(N)
Vec_wei.setrand(weight)
 
 
objref Lis_motoneuron,Lis_motoneuronI
Lis_motoneuron=new List()
Lis_motoneuronI=new List()
objref SF[N]  
 
//************************************************************** 
 
// Postsynaptic neuron  
for i = 0, 10 {         //    N1      N1-1        
SF[i] = new F_1()         //1 - 4 ( 0 - 3)
}                         
                          
for i = 11,21 {       //    N2      N2-1
SF[i] = new F_2()         //7 - 14( 6 - 13)
}                         

for i = 22, 32{        //      N3     N3-1
SF[i] = new F_3()          //17 - 24(16 - 23)
}


for i = 33, 43 {        //     N4      N4-1
SF[i] = new F_4()          //27 - 40(26 - 39)
}


for i = 44, 54 {         //      N5       N5-1
SF[i] = new F_5()          //  43 - 57 (42 - 56)
}


for i = 55, 65 {         //      N6       N6-1
SF[i] = new F_6()          //  61 - 75 (60 - 74)
}


for i = 66, 76{        //       N7       N7-1
SF[i] = new F_7()          //  78 - 85 (77 - 84)
}
                               
for i = 77 , 87 {        //       N8       N8-1
SF[i] = new F_8()          //  87 - 93 (86 - 92)
}

for i = 88,99{        //        N9      N9-1
SF[i] = new F_9()          //   95 - 99(94 - 98)
}
 
//**************************************************************

objectvar syn[N]              
objectvar synI[N]              
objectvar synR[N] 


for j=0,N-1{ 
SF[j].soma syn[j] = new Exp2Syn1(1)
syn[j].tau1=t1 //--- ms rise time

syn[j].tau2=t2 //--- ms decay time

syn[j].e=0 //-- mV reversal potential

syn[j].gmax=Gsyn //-- uS synaptic current
 
}

for j=0,N-1{ 
SF[j].soma synI[j] = new Exp2Syn1(0.5)
synI[j].tau1=t1//--- ms rise time

synI[j].tau2=t2 //--- ms decay time

synI[j].e=-100 //-- mV reversal potential

synI[j].gmax=GsynR //-- uS synaptic current
 
}


//Presynaptic stimulation
objectvar Snet

Snet = new NetStim1(0.5)

Snet.interval=10  //ms (mean) time between spikes

Snet.number=120 //(average) number of spikes

Snet.start=200 //ms (most likely) start time of first spike

Snet.noise=0.5// ---- range 0 to 1. Fractional randomness.

 
//**************************************************************

objectvar netcon[N]
objectvar netconI[N] 
objectvar netconR[N] 
for j=0,N-1{                                                  
netcon[j] = new NetCon(Snet, syn[j], 0,(j+1)*3, Vec_wei.get(j))  
 
}                          
 
 
for j=0,N-1{                                                  
netconI[j] = new NetCon(Snet, synI[j], 0,Vec_dey.get(j),Vec_wei.get(j)) 
 
}  

 
//********************************************************************************
objref Vec_Vol[N], Vec_Tim ,Fil_Vol[N],Fil_Tim[N] ,Vec_Cont ,Vec_ContT[N] ,Vec_TE, Vec_TI  ,Vec_CT

 Vec_CT = new Vector()
  for j=0,N-1{ 
   Vec_Vol[j] = new Vector()
 
}

 Vec_Tim  = new Vector()
  for j=0,N-1{ 
   Fil_Vol[j] = new File()
   Fil_Tim[j] = new File()
}
 
 Vec_Cont =  new Vector()
  for j=0,N-1{
   Vec_ContT[j] =  new Vector()
}

 Vec_TE =  new Vector()
 Vec_TI =  new Vector()
  for j=0,N-1{   
   Vec_Vol[j].record(&SF[j].soma.v(0.5)) 
   Vec_Tim.record(&t)
}

//______________________________________________________
 init()
 run()
//______________________________________________________

  
 for (i= 0;i<=Vec_Tim.size()-1;i=i+1){        
     if(i+1<Vec_Tim.size()){
     count=0
     for j=0,N-1{                            
        if(((Vec_Vol[j].get(i+1)<Vth&&Vec_Vol[j].get(i+1)>Vm)||(Vec_Vol[j].get(i)>Vth))||((Vec_Vol[j].get(i)<Vth&&Vec_Vol[j].get(i+1)>Vm))){
        count+=1
      }
    
     }    
  }
    Vec_Cont.append(count) 
    if(i!=0){
      if(Vec_Cont.get(i)!=Vec_Cont.get(i-1)) {
      Vec_CT.append(count)
    }
  }   
       
} 
  
 objref g ,CF,CT , vec
  vec=new Vector()
  vec.indgen(Vec_ContT.min(),Vec_ContT.max(),(Vec_ContT.max()-Vec_ContT.min())/100)
  g=new Graph()
  g.size(0,80000,0,50)
  Vec_Cont.plot(g)


/*
 objectvar stim[N]
  for i=0,N-1{   
  SF[i].soma stim[i] = new DC(0.5)     
} */ 



/*objectvar ftriangle  
 
Spre.soma  ftriangle = new RampIClamp(0.5) 
      
     
ftriangle.amp[0]=0
ftriangle.dur[0]=1000
ftriangle.amp[1]=3
ftriangle.dur[1]=5000 
ftriangle.amp[2]=0
ftriangle.dur[2]=5000 
 tstop=12000  */                  
 
 
/* objectvar  Step[N]
 for i=0,N-1{   
 SF[i].soma Step[i] = new IClamp(0.5)  
Step[i].del=100
Step[i].dur=150
Step[i].amp=17
}
tstop=300    */

 

//load_file("pool.ses")