begintemplate Granule

public soma, dend, gemmshaft, gemmbody
public AMPAr, NMDAr,GABAAr,spiketimes, dendspike

create soma, dend, gemmshaft, gemmbody
objref AMPAr, NMDAr, GABAAr
objref spiketimes, dendspike, spikecount, dendcount

proc celldef() {
	topol()
	subsets()
	geom()
	biophys($1)
	geom_nseg()
}

proc topol() { 
	connect dend(0), soma(1)
	connect gemmshaft(0), dend(1)
	connect gemmbody(0), gemmshaft(1)	
}

objref all, gcdendgemm

proc subsets() {
	all = new SectionList()
	soma all.append()
	dend all.append()
	forsec "gemm" all.append()
	
	gcdendgemm = new SectionList()
	forsec "dend" gcdendgemm.append()
	forsec "gemm" gcdendgemm.append()
}

proc geom() {
	soma { L = 8   diam  = 8 } 
	dend { L = 150 diam  = 1 } 
	gemmshaft { L=1 diam =1 }
	gemmbody  { L=1 diam =1 }
}

proc geom_nseg() { 
	forall {
	 //nseg = 1
	 // nseg = int((L/(0.1*lambda_f(100))+0.9)/2)*2 + 1
    }	
	soma { nseg = 1 }
	dend { nseg = 1 }    //3
	gemmshaft { nseg = 1 }
	gemmbody  { nseg = 1 }
}


func lambda_f() {
  return  1e5*sqrt(diam/(4*PI*$1*Ra*cm))
}

proc biophys() { local AMPAtau, NMDAalpha, NMDAbeta, Erev, GABAAtau,GABAArev

    spiketimes = new Vector()
	dendspike  = new Vector()
	
	RM	= 30e3  	// ohm.cm2   
        EL 	= -60		// -75 mV
	
	ENA =  45
        EK  = -80	    // -75/-80 !!!
	ECA = 100
	
	NMDAalpha = 0.0163	// ms-1
        NMDAbeta  = 0.00292	// ms-1
        AMPAtau  = 5.5		// ms
        Erev	 = 0		    // mV
	GABAAtau = 8.3 		//  
        GABAArev = -70
	
	spike_threshold = -10
	
	//=================================
	
	gNa_Soma = 50e-3  // 50 !!!
	gNa_Dend = 20e-3  // 20
	   Sh_Na = 5      // 10 !!!    
	
	gKdr_Soma = 20e-3  // 20
	gKdr_Dend =  5e-3  //  5	
	
	gA_Soma = 20e-3   //  20 !!!
	gA_Dend = 60e-3   //  60
	  Sha_A =  -0     //  9.9 / -2   !!!
	  Shi_A =  -20    //  5.7 / -20  !!!
	  k_tauH  = 2.5   // 2!!!
	  sh_tauH = 0
	
	gM_Soma    = 0.5e-3    // 0.5 !!!
	gM_Dend    = 0.0e-3    // 0	
	
	gKCa_Soma  = 0e-3    // 
	gKCa_Dend  = 0.5e-3  //  0.5 !!! 
	
	gCaT_Dend  = 0.1e-3  //  
	
	gCapn_Dend = 0.2e-3	 // 
	
	gcan_Dend  = 1.0e-3  // 0.8 !!!
	
	
	if ($1==0) {
	   gM_Soma    = 0.5e-3
	   gKCa_Dend  = 0.5e-3  //  
	} else {
	   gM_Soma    = 0.0e-3
	   gKCa_Dend  = 0.0e-3  //  
	}
		
	shell_depth = 0.2    // um !!!
	
	forall {
     Ra = 80     // ohm*cm; 
     cm = 2.0    // 1/1.5 !!! uF/cm^2;
    }  
	
	soma {
	insert pas 
		g_pas = 1/RM
		e_pas = EL
	
	insert nax
        gbar_nax  = gNa_Soma
        sh_nax  = Sh_Na	
/*	
    insert nafast
        gnabar_nafast  = gNa_Soma   
		sh_nafast = Sh_Na
*/
	insert kdrmt
	    gbar_kdrmt = gKdr_Soma    
	insert kamt
        gbar_kamt = gA_Soma    // 
		sha_kamt  = Sha_A 
		shi_kamt  = Shi_A
		k_tauH_kamt = k_tauH
		sh_tauH_kamt = sh_tauH
    insert kM
        gkbar_kM  = gM_Soma    // 	
    
	insert Ikca
        gkbar_Ikca = gKCa_Soma
	
	insert cad2     
        depth_cad2  = shell_depth
	ena = ENA
	ek  = EK
	
	spikecount  = new APCount(0.5)
    spikecount.thresh = spike_threshold
    spikecount.record(spiketimes)
	
	}
	
	forsec gcdendgemm {
	insert pas 
		g_pas = 1/RM
		e_pas = EL

	insert nax
        gbar_nax = gNa_Dend	
		sh_nax  = Sh_Na
/*	
	insert nafast
        gnabar_nafast  = gNa_Dend   
		sh_nafast = Sh_Na
*/	
    insert kdrmt
	    gbar_kdrmt = gKdr_Dend   
	insert kamt
        gbar_kamt  = gA_Dend    // 
		sha_kamt = Sha_A 
		shi_kamt = Shi_A
		k_tauH_kamt = k_tauH
		sh_tauH_kamt = sh_tauH		
	insert kM
        gkbar_kM  = gM_Dend    // 
	
	insert Icat
	    gbar_Icat = gCaT_Dend 
	insert Icapn
        gbar_Icapn = gCapn_Dend	
	insert Ican
	    gbar_Ican = gcan_Dend 
	insert Ikca
        gkbar_Ikca = gKCa_Dend  	    
	
	//insert Cacon	
	
	insert cad2     
      depth_cad2  = shell_depth
	
	ena = ENA
	ek  = EK
	eca = ECA
  }	

  gemmbody{
	AMPAr = new ExpSyn(0.5)
    AMPAr.tau 	= AMPAtau
    AMPAr.e 	= Erev
    NMDAr = new NMDA(0.5)
    NMDAr.Alpha	= NMDAalpha
    NMDAr.Beta	= NMDAbeta
    NMDAr.e		= Erev
	
	GABAAr = new ExpSyn(0.5)
    GABAAr.tau 	= GABAAtau
    GABAAr.e 	= GABAArev
	
	dendcount = new APCount(0.5)
    dendcount.thresh = spike_threshold
    dendcount.record(dendspike)
	
  }
  	
}

//access soma
proc init() {
celldef($1)

}

endtemplate Granule