// granule.tem
// Template for granule cell model
// Andrew Davison, The Babraham Institute, 2000


begintemplate Gran
  
  public soma, proxi, dist, gemm, ngemm, AMPAr, NMDAr, AMPArS, NMDArS, CaSyn, spiketimesS, spikecountS, spiketimes, spikecount//,r

  ndist = 5 
  //print "ndist=", ndist
  ngemm = 30              //valeur par defaut superieure a la realite.

  create soma, proxi, s2p, p2d[ndist], dist[ndist], d2g[ngemm], gemm[ngemm]
  objref AMPAr, NMDAr, AMPArS[ngemm], NMDArS[ngemm], CaSyn[ngemm], spiketimes, spikecount, spiketimesS[ngemm], spikecountS[ngemm], randspine, specspine

  proc init() { local Len, Erest, RM, p, q, Atotal, AMPAtau, NMDAalpha, NMDAbeta, Erev, k


    create soma, proxi, s2p, p2d[$1], dist[$1], d2g[$2], gemm[$2]

    ngemm = $2

    spiketimes = new Vector()
    lastspikecount = 0
  
    for i = 0, ngemm-1 {
	spiketimesS[i] = new Vector()
    }


// attribution d'une specificite a chaque epine
    randspine = new Random()
    specspine = new Vector(ngemm)
    for k=0, ngemm-1 {
	specspine.x[k] = randspine.discunif(0,5)
        //print specspine.x[k]
    }

    ndist = $1
    ngemm = $2
 
    Erest		= -65		// mV
    Atotal		= 8353		// um2

    RM			= 4000//100000  //4000	// ohm.cm2 //Davison versus anton Granger

    Ri			= 70 		//ohm.cm
    Len			= 50
    p			= 0.0136
    q			= 0.308

    NMDAalpha		= 0.0163	// ms-1
    NMDAbeta		= 0.00292	// ms-1
    AMPAtau		= 3		// ms
    Erev		= 0		// mV
 	//   r = 0.4

   soma {
      	L			= Len
      	diam		= p*Atotal/(PI*Len)
      	Ra		= 80 // ohm.cm Anton Granger 1993 
      	insert pas
      	e_pas		= Erest		// reversal potential mV
      	g_pas		= 1/RM 		// membrane conductance S/cm2
      	insert nagrantab
      	insert kslowtab
      	insert kM
      	insert kA
	//inserer Canaux T, canaux N and P/Q. Egger, Pinato's, Isaacson98
      	gnabar_nagrantab 	= 0.1611	// S/cm2
      	gkbar_kslowtab 	= 0.1313
      	gkbar_kM 		= 0.1334
      	gkbar_kA 		= 0.0088
    }

   s2p {
       diam 		= 1
       Ra 		= 80 // ohm.cm Anton Granger 1993 
       L 		= 1
   }

   proxi {
      L 		= 150
      diam 		= q*Atotal/(PI*Len)
      Ra 		= PI/(4*Len*Atotal)
      insert pas
      e_pas 		= Erest
      g_pas 		= 1/RM
      insert nagrantab
      insert kslowtab
      gnabar_nagrantab 	= 0.1355
      gkbar_kslowtab 	= 0.0243
    }

   for i = 0, ndist-1 {
       p2d[i] { 
           diam 	= 1
           Ra 		= 8000000//80  // ohm.cm Anton Granger 1993 
           L 		= 1
    }
   }

   for i = 0, ndist-1 {
	dist[i] {
      		L 		= 50  // um Anton Granger 1993
      		diam 		= 1   // um Anton Granger 1993
      		Ra 		= 80  // ohm.um Anton Granger 1993 
      		insert pas
      		e_pas 		= Erest
      		g_pas 		= 1/RM
//      		insert nagrantab
//      		insert kslowtab
//      		gnabar_nagrantab 	= 0.1355
//      		gkbar_kslowtab 	= 0.0243
     	}
    }

// spine neck
    for i = 0, ngemm-1 {
	d2g[i] {
	   diam 	= 0.2     // um Anton Granger 1993
           Ra 		= 150*10   // ohm.um Anton Granger 1993 
           L 		= 3       // um Anton Grnager 1993 
        }
    }

// spine
    for k = 0, ngemm-1 {
	gemm[k] {
		
	    r           = 0.4
	    L 		= 1.747 * r 		//conversion sphere/cylindre
            diam	= 2*r
	    Ra 		= 80 //  ohm.um Anton Grnager 1993 

            insert pas
            e_pas 		= Erest		// 
            g_pas 		= 1/RM 		// 1/specspine.x[k]   	// a rectifier

            insert nafast
            insert kfasttab
            insert kslowtab
     	    //insert lcafixed
    	    //insert cad
	    //depth_cad 	= 8

	    //insert itGHK
	    //insert N_Ca	 	// lire Kerry/Delaney CAN
	    //insert L_HVA_Ca	// Halabisky

	    // insert kA 	//relire Christie et Westbrook qui en parlent
	    // inserer canaux P/Q, cad. Isaacson98
	    // IA Schoppa W 1999.

            gnabar_nafast 	= 0     //0.1532
            gkbar_kfasttab 	= 0//0.1   //*specspine.x[k]//0.1956
            gkbar_kslowtab 	= 0//0.243

            //gcabar_lcafixed 	= 0     // 0.01         //0.0040
	    //pcabar_itGHK        = 0     // 0.2e-3 	//permeability cm/s
	    //gcabar_N_Ca		= 0     // 0.02  	//This is the original value(mho/cm2)   
	    //gcabar_L_HVA_Ca	= 0     // 0.0003 	//original = 7e-05(mho/cm2) 

            AMPArS[k] = new ExpSyn(0.5)    	//not taken into account but can be an option later
            AMPArS[k].tau 	= AMPAtau  	//idem
            AMPArS[k].e 	= Erev		//idem
            NMDArS[k] = new NMDA(0.5)		//idem
            NMDArS[k].Alpha	= NMDAalpha	//idem
            NMDArS[k].Beta	= NMDAbeta	//idem
            NMDArS[k].e		= Erev		//idem
            spikecountS[k] = new APCount(0.5)	
            spikecountS[k].thresh = -35		
            spikecountS[k].record(spiketimesS[k])
        }
    }
 
   // set reversal potentials, etc.
   forall if (ismembrane("na_ion")) {
             ena = 45	// mV
          }

   forall if (ismembrane("k_ion")) {
          ek  = -70	//  mV
   }

    forall if (ismembrane("ca_ion")) {
      eca = 70		// mV 
      cai = 0.00001	// mM 
      cao = 2		// mM
      ion_style("ca_ion",3,2,0,0,1)
    }

   // Connexion de toute la cellule

   connect s2p(0), soma(1)
   connect proxi(0), s2p(1)

   for i=0, ndist-1  connect p2d[i](0), proxi(1)
   for i=0, ndist-1  connect dist[i](0), p2d[i](1) 

 //  for i=0, ngemm-1 connect d2g[i](0), dist[int(i*ndist/ngemm)](1) //On peut choisir aussi de repartir les epines selon Woolf,G,S 1991
   for i=0, ngemm-1 connect d2g[i](0), dist[0](1) 
   for i=0, ngemm-1 connect gemm[i](0), d2g[i](1) 

}

endtemplate Gran