load_file("nrngui.hoc")                         //1
load_file("../templates/FS_WT_modif.tem")       //2
//load_file("../templates/FS_temp.tem")         //2
//load_file("../templates/Pyram_WT_lascon.tem") //3
load_file("../templates/Pyram_WT_modif.tem")    //3
//load_file("../templates/templ_pyr1a.tem")     //3
load_file("../connections/Def_connec_bgk2.hoc") //4
//load_file("../../connections/Def_connec_bgk2_beta.hoc")
//*****************************************************************************************
//*****************************************************************************************
n_P=80        //number of Pyramidal cells                  ***gamma oscill if         ***
n_FS=20       //Number of fast spiking                     ***AminE=20e-3,AmaxE=25e-3,***
n_layerP=2      //number of pyr_cells layers                 ***AminI=2e-3, AmaxI=5e-3  ***
n_layerFS=2     //number of fs_cells layers                  ***Wie=9e-3  Wii=1e-2      ***
IEperc=0.6      //0.5//1//0.3      //percentage of  IE  presinaptic inputs
Wie=2.0*5.02e-3//5e-3// 3*5.02e3pS/1257um2 =12pS/um2      4e-2        //5e-3//0.04//0.005//synaptic weight (al previos simulations 0.04)
SDwie=0.001     //Standard Deviation
EEperc=0.3      //percentage of  EE  presinaptic inputs
Wee=5e-2//5e-2//0.05  //synaptic weight =>g=Wee*1.257e-3(uS)
SDwee=0.005     //Standard Deviation
IIperc=0.55     //                   0.55//0.52      //percentage of  II  presinaptic inputs
Wii=10*5.02e-3//5e-3// 3*5.02e3pS/1257um2 =12pS/um2               |||| ==>original       //7e-3  //0.007//0.01        //synaptic weight=g (uS)
SDwii=0.002     //Standard Deviation
EIperc=0.65//0.6      //percentage of  EI  presinaptic inputs
Wei=3e-2//0.05       //synaptic weight =>g=Wei*1.257e-3(uS)
SDwei=0.003     //Standard Deviation
nSpikesE=0      //tstop/interv(not in use yet by itself)
nSpikesI=0      //tstop/iterv(not in use yet by itself)
w_spk_I=3.3e-4  //0//0.000035 // (uS) max conductance of artif Spike synapse to I cells 
w_spk_E=3.3e-4  //0.00070 //(uS)  max conductance of artif Spike synapse to E cells
interval_E=9e10 //average inter_random_interspikes interval (100 ms originally)
interval_I=9e20//2.8e2  //6.5e10//(original  100ms=10Hz)
CCh_fct=1//.5
tau_slow=6.8              //gaba decay fast
tau_fast=3.5              //gaba decay slow


AminE=8e-6*CCh_fct//0.4//0.8//nA min Amplitude external baseline to E cell (scaled in instruments file) ** to get real value  in nA
AmaxE=9e-6*CCh_fct//0.6//0.9 //nA max Amplitude external baseline to E cell                             ** multiply the amplitude without e-3
AminI=.7e-6*CCh_fct//nA min Amplitude external baseline to I cell                                         ** by the factor 0.0012566371
AmaxI=2e-6*CCh_fct//nA max Amplitude external baseline to i cell

geI_factor=0//multiplicative factors of the 
giI_factor=0//conductances used to connect 
geE_factor=0.8//intersecting cells of both networks
giE_factor=1//

gEi_factor=0
gIi_factor=0
gEe_factor=0
gIe_factor=0

//====================================================================
//=============================connection percentage==================
geI_percent=1 //multiplicative factors of the 
giI_percent=1//percentage of connections
geE_percent=1//intersecting cells of both networks only changes that related to the intersting connection
giE_percent=1//.

gEi_percent=1
gIi_percent=1
gEe_percent=1
gIe_percent=1

//====================================================================
//=============================connection flags=======================
if(geI_factor>0){geI_f=1} 
if(giI_factor>0){giI_f=1} 
if(geE_factor>0){geE_f=1}
if(giE_factor>0){giE_f=1}

if(gEi_factor>0){gEi_f=1}
if(gIi_factor>0){gIi_f=1}
if(gEe_factor>0){gEe_f=1}
if(gIe_factor>0){gIe_f=1}
//=============================connection flags=======================
//====================================================================


strdef vrc_flag1,vrc_flag2,vrc_flag3,vrc_flag4
//*********************************************\\*//*******************************************
sub_spkE=0 //number of additional independent spk stream to Epop 
sub_spkI=0 //number of additional independent spk stream to Ipop
intv_hfE=1e0//Additional Driving Spk stream period (ms) if any
intv_hfI=1e1//Additional Driving Spk stream period (ms) if any   
w_add_I=3e-4   //synaptic weight of additional stream to I cells
w_add_E=3e-4   //synaptic weight of additional stream to E cells
load_file("../Instrum_Visualize/Instrument_and_visualize_100509.hoc")//5 External drive
tstop=4e4   //CHANGE THIS VALUE TO 2000
tmin=0e2          //initial time for voltage traces recordings             
tmax=tstop           //final time for voltage traces recordins     
//*****************************************************************************************
n_SgabaP=n_layerP*n_P*n_layerFS*n_FS     //Number of gaba synapses in Pyramidal
n_SgabaFS=n_layerFS*n_FS*n_layerFS*n_FS  //Number of gaba synapses in FS
n_SnmdaP=n_layerP*n_P*n_layerP*n_P       //Number of Glutamatergic (nmda) synapses in Pyram
n_SnmdaFS=0                              //Number of Glutamatergic (nmda) synapses in fast spiking cells
n_sAMPAp=n_layerP*n_P*n_layerP*n_P       //Number of Glutamatergic (AMPA) synapses in Pyram
n_sAMPAfs=n_layerP*n_P*n_layerFS*n_FS    //Number of Glutamatergic (AMPA) synapses in fast spiking cells
tresh1=0 //GABA  I-E I-I                 
tresh2=0 //AMPA  E-E E-I
tresh3=0  //NMDA  E-E          problems  when higher number of cells and  threshold =-45mV
//*****************************************************************************************
//*****************************************************************************************
objref layerP[n_layerP] //array containing  pyramidal cell lists (layers)
objref SgabaP[n_layerP],SnmdaP[n_layerP],SampaP[n_layerP]
objref layerFS[n_layerFS] //array containing each fs cell lists (layers)
objref SgabaFs[n_layerFS],SnmdaFS[n_layerFS],SampaFS[n_layerFS]
objref fih0,cvd
//cvd=new CVode()
fih0=new List()
//******************************************************************************
//****************************CELLS CREATION************************************
if(n_P>0){
    for(k=0;k<n_layerP;k+=1){
        layerP[k]=new List()        
        SgabaP[k]=new List()
        SnmdaP[k]=new List()
        SampaP[k]=new List()        
        create_pyrams(layerP[k],n_P) //2   
    }
}
if(n_FS>0){
    for(j=0;j<n_layerFS;j+=1){
        layerFS[j]=new List()
        SgabaFs[j]=new List()
        SnmdaFS[j]=new List()        
        SampaFS[j]=new List()
        create_FS(layerFS[j],n_FS)
          
    }
}
//******************************************************************************
//*****************CONNECTIONS TO PYRAMIDAL*************************************
    if(n_P>0&&n_layerP>0){
        for(i=0;i<n_layerP;i+=1){
         /* mrsE=100 //maximun number of random spikes to E cells            
           if(i<int(0.2*n_P)){interval_E=int(0.2*mrsE)}
            if(i>int(0.2*n_P)&&i<=int(0.4*n_P)){interval_E=int(0.4*mrsE)}
            if(i>int(0.4*n_P)&&i<=int(0.6*n_P)){interval_E=int(0.6*mrsE)}
            if(i>int(0.6*n_P)&&i<=int(0.8*n_P)){interval_E=int(0.8*mrsE)}
            if(i>int(0.8*n_P)&&i<n_P){interval_E=mrsE} */
        num_e=1/4    
	createSpikeGen(layerP[i],interval_E,num_e,80,0.95,0,2,AminE,AmaxE,0,i,w_spk_E,sub_spkE,intv_hfE,w_add_E)//default 15 args
	if(i==3||i==4||i==6||i==7){createSpikeGen(layerP[i],interval_E,num_e,80,0.95,0,2,0,0,0,i,w_spk_E,sub_spkE,intv_hfE,w_add_E,0)}  //popul. ohne AP trains      
	
	if(i==0){tau_g=tau_slow}else if(i==1){tau_g=tau_fast}		//eran 7 y 1.7 ms
	mk_GABA_s(n_SgabaP, n_P,0,0, 0.5,layerP[i], SgabaP[i], 1,1,tau_g)
        //mk_NMDA_s(n_SnmdaP, n_P,0,0, 0.5,layerP[i], SnmdaP[i], 0)
        mk_AMPA_s(n_sAMPAp, n_P,0,0, 0.5,layerP[i], SampaP[i], 0)
        for(j=0;j<n_layerFS;j+=1){
            if(j==i){                                                  
          // conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,Wie*(1+10^(j+1)),layerFS[j],layerP[i],SgabaP[i],IEperc)
	            conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,Wie,layerFS[j],layerP[i],SgabaP[i],IEperc)
	            //if(j==i&&i==1)conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,Wie*500,layerFS[j],layerP[i],SgabaP[i],IEperc)
     vrc_flag1=vrc_flag
           }
	if(j<i && giE_f==1){
//iE==>                              
        //conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,giE_factor*Wie*(1+10^(j+1)),layerFS[j],layerP[i],SgabaP[i],0.15*giE_percent*IEperc)
        conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,giE_factor*Wie,layerFS[j],layerP[i],SgabaP[i],0.15*giE_percent*IEperc)
        //,Wie,SDwie)//1) 
	   vrc_flag1=vrc_flag
           }
	if(j>i && gIe_f==1){                              
 //Ie==>
        //conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,gIe_factor*Wie*(1+10*(i+1)),layerFS[j],layerP[i],SgabaP[i],0.15*gIe_percent*IEperc)//,Wie,SDwie)//1)
        conn_src_targ(n_P*n_FS,n_P,n_FS,0,tresh1,gIe_factor*Wie,layerFS[j],layerP[i],SgabaP[i],0.15*gIe_percent*IEperc) 
	   vrc_flag1=vrc_flag
              }
           }
        for(k=0;k<n_layerP;k+=1){        
        if(k==i){
           //percentage of  EE  presinaptic inputs
 	   conn_src_targ(n_P*n_P,n_P,n_P,0,tresh1,Wee,layerP[k],layerP[i],SampaP[i],EEperc)//,Wee,SDwee)//0.5000011
           vrc_flag2=vrc_flag
            }
           if(k<i && geE_f==1){
//eE==>
          conn_src_targ(n_P*n_P,n_P,n_P,0,tresh1,geE_factor*Wee,layerP[k],layerP[i],SampaP[i],0.15*geE_percent*EEperc)// 0.4)//connections inter-networks to large taus
        		}
       	  if(k>i && gEe_f==1){
//Ee==>
	  conn_src_targ(n_P*n_P,n_P,n_P,0,tresh1,gEe_factor*Wee,layerP[k],layerP[i],SampaP[i],0.15*gEe_percent*EEperc)        	
                            }                   
                          }
                        }
                      }
//*****************CONNECTIONS TO FAST SPIKING**********************************
//******************************************************************************

if(n_FS>0&& n_layerFS>0){
    for(i=0;i<n_layerFS;i+=1){
        /* mrsI=100 //maximun number of random spikes to I cells            
            if(i<int(0.2*n_FS)){interval_I=int(0.2*mrsI)}
            if(i>int(0.2*n_FS)&&i<=int(0.4*n_FS)){interval_I=int(0.4*mrsI)}
            if(i>int(0.4*n_FS)&&i<=int(0.6*n_FS)){interval_I=int(0.6*mrsI)}
            if(i>int(0.6*n_FS)&&i<=int(0.8*n_FS)){interval_I=int(0.8*mrsI)}
            if(i>int(0.8*n_FS)&&i<n_FS){interval_I=mrsI}*/

	if(i==0){tau_g=tau_slow isii_fct=1}else if(i==1){tau_g=tau_fast isii_fct=1}
//	if(i==0){tau_g=10/3 AminI=1.8e-6*CCh_fct AmaxI=3.2e-6*CCh_fct isii_fct=1e-19}else if(i==1){tau_g=5/3 isii_fct=1}	//original slow and fast tau_g=10/3  tau_g=2.1/3   
            num_i=100//tstop/6
            //createSpikeGen(layerFS[i],interval_I,num_i,80,1,0,2,AminI,AmaxI,1,i)//
            createSpikeGen(layerFS[i],(interval_I*isii_fct),num_i,80,0,0,2,AminI,AmaxI,1,i,w_spk_I,sub_spkI,intv_hfI,w_add_I)//
            mk_GABA_s(n_SgabaFS, n_FS,0,0, 0.5,layerFS[i],SgabaFs[i],0,1,tau_g)
            mk_AMPA_s(n_sAMPAfs,n_FS,0,0,0.5,layerFS[i],SampaFS[i],1)

    //============================================================================== 
    //============================================================================== 
        for(i1=0;i1<n_layerFS;i1+=1){
            if(i1==i){                
            ///conn_src_targ(n_FS*n_FS,n_FS,n_FS,0,tresh1,Wii*(1+10^(i1+1)),layerFS[i1],layerFS[i],SgabaFs[i],IIperc)//,Wii,SDwii)
            conn_src_targ(n_FS*n_FS,n_FS,n_FS,0,tresh1,Wii,layerFS[i1],layerFS[i],SgabaFs[i],IIperc)//without multiplying by 100 or 1000
            vrc_flag3=vrc_flag
            }
	    if(i1<i && giI_f==1 ){ 
//iI==>                              
            //conn_src_targ(n_FS*n_FS,n_FS,n_FS,0,tresh1,giI_factor*Wii*(1+10^(i1+1)),layerFS[i1],layerFS[i],SgabaFs[i],0.15*giI_percent*IIperc)//,Wie,SDwie)//1)
            conn_src_targ(n_FS*n_FS,n_FS,n_FS,0,tresh1,giI_factor*Wii,layerFS[i1],layerFS[i],SgabaFs[i],0.15*giI_percent*IIperc) 
	    vrc_flag1=vrc_flag
            }
	    if(i1>i && gIi_f==1 ){
	    //conn_src_targ(n_FS*n_FS,n_FS,n_FS,0,tresh1,gIi_factor*Wii*(1+10^(i+1)),layerFS[i1],layerFS[i],SgabaFs[i],0.15*gIi_percent*IIperc)
      conn_src_targ(n_FS*n_FS,n_FS,n_FS,0,tresh1,gIi_factor*Wii,layerFS[i1],layerFS[i],SgabaFs[i],0.15*gIi_percent*IIperc)                             
	    vrc_flag1=vrc_flag
             	}
       	    }
	for(j=0;j<n_layerP;j+=1){        
            if(j==i){
           conn_src_targ(n_P*n_FS,n_FS,n_P,0,tresh1,Wei,layerP[j],layerFS[i],SampaFS[i],EIperc)//,Wei,SDwei)
            vrc_flag4=vrc_flag
            }
            if(j<i && geI_f==1){
//eI==> 
            conn_src_targ(n_P*n_FS,n_FS,n_P,0,tresh1,geI_factor*Wei,layerP[j],layerFS[i],SampaFS[i],0.15*geI_percent*EIperc)
             }
            if(j>i && gEi_f==1){
            conn_src_targ(n_P*n_FS,n_FS,n_P,0,tresh1,gEi_factor*Wei,layerP[j],layerFS[i],SampaFS[i],0.15*gEi_percent*EIperc)
                        }                        
                      }
                    }         
                   }
//*****************CONNECTIONS TO FAST SPIKING**********************************//
//******************************************************************************//
/*
load_file("./in_process/Ext_Spks_counter.hoc") 
load_file("./sessions/Spk_rec_pre.hoc")
load_file("./sessions/Spk_rec_post.hoc")
*/