//////////////////* Cells Template *//////////////////

//Parameters Setting 
SOMA_SIZE_L = 28
SOMA_SIZE_diam = 28 // assume spherical shape for soma
SOMA_Ra = 200 // Ra, [ohm-cm] Cell resistance
SOMA_cm = 1 //cm,  [uF/cm2] Membrane capacitance
//For Hodgkin-Huxley model
GNaBAR_HH = 0.12 //gnabar_hh, [mho/cm2](mho = ohm^-1)  Maximum specific sodium channel conductance
GKBAR_HH = 0.036 //gkbar_hh, [mho/cm2]   Maximum potassium channel conductance
GL_HH_ZERO = 0 //gl_hh, [mho/cm2]  Leakage conductance in HH model -> set to zero because the mechanism "pas" is used
EL_HH = -65 //el_hh , [mV]       Leakage reversal potential
GL_PAS = 0.0004 //gl_hh, [mho/cm2]  Leakage conductance 
EL_PAS = -65 //el_hh , [mV]       Leakage reversal potential
ENa_HH = 55 //ena, [mV]  Na reversal potential
EK_HH = -80 //ek, [mV]   K reversal potential
//Calcium T-Type
ECa_CaT = 126.1 //eca, [mV]   Ca reversal potential
GCaMAX_CaT = 0.002 //gmax_CaT [mho/cm2]  Maximum specific calcium channel conductance
//netcon
NETCON_THRESHOLD = 0 //nc.threshold --> When the source variable passes threshold in the positive direction at time t-delay, the target will receive an event at time t along with weight information. 
//synapses
EPSP_TAU1 = 1 //[ms]
EPSP_TAU2 = 3 //[ms]
EPSP_E_REVERSE = 0 //[mV]
IPSP_TAU1 = 1 //[ms]
IPSP_TAU2 = 7 //[ms]
IPSP_I_REVERSE = -80 //[mV]

///////////// Templates

begintemplate Target_Cell //////////////////////////////////////////////////////////Begin Target_Cell template 
public is_art
public init, topol, basic_shape, subsets, geom, biophys, geom_nseg, biophys_inhomo
public synlist, cID, tID, cType, x, y, z, position, connect2target, setID   //cID = cell ID (numbering of all cells) , tID = type ID 

public soma
public all

objref synlist
external  SOMA_SIZE_L, SOMA_SIZE_diam, SOMA_Ra,SOMA_cm, GNaBAR_HH, GKBAR_HH, GL_HH_ZERO, EL_HH, ENa_HH , EK_HH, NETCON_THRESHOLD 
external EPSP_TAU1,EPSP_TAU2,EPSP_E_REVERSE ,IPSP_TAU1,IPSP_TAU2, IPSP_I_REVERSE, EL_PAS, GL_PAS
external ECa_CaT, GCaMAX_CaT 

proc init() {
  topol()
  subsets()
  geom()
  biophys()
  geom_nseg()
  synlist = new List()
  synapses()
  x = y = z = 0 // only change via position
}

create soma

proc topol() { local i
  basic_shape()
}
proc basic_shape() {
  soma {pt3dclear() pt3dadd(0, 0, 0, 1) pt3dadd(15, 0, 0, 1)}
}

objref all
proc subsets() { local i
  objref all
  all = new SectionList()
    soma all.append()

}
proc geom() {
  forsec all {  L = SOMA_SIZE_L diam = SOMA_SIZE_diam  } //{  L =  SOMA_SIZE  diam =  SOMA_SIZE  } //um  
}
external lambda_f
proc geom_nseg() {
  forsec all { nseg = int((L/(0.1*lambda_f(100))+.999)/2)*2 + 1  }
}
proc biophys() {
  forsec all {
	Ra = SOMA_Ra //ohm-cm
	cm = SOMA_cm //uF/cm2
    insert hh
      gnabar_hh = GNaBAR_HH  //GNaBAR_HH //0.12
      gkbar_hh = GKBAR_HH  //GKBAR_HH //0.036 
      gl_hh = GL_HH_ZERO  //GL_HH_ZERO //0.0001
      el_hh = EL_HH //EL_HH //-60
	  ena = ENa_HH
	  ek = EK_HH
	insert pas
		e_pas = EL_PAS
		g_pas = GL_PAS
	insert CaT
		gmax_CaT = GCaMAX_CaT
		eca = ECa_CaT
  }
}
proc biophys_inhomo(){}
proc position() { local i
  soma for i = 0, n3d()-1 {
    pt3dchange(i, $1-x+x3d(i), $2-y+y3d(i), $3-z+z3d(i), diam3d(i))
  }
  x = $1  y = $2  z = $3
}
obfunc connect2target() { localobj nc //$o1 target point process, optional $o2 returned NetCon
  soma nc = new NetCon(&v(0.5), $o1)
  nc.threshold = NETCON_THRESHOLD
  if (numarg() == 2) { $o2 = nc } // for backward compatibility
  return nc
}
proc setID(){ //$1 = cell ID , $2 = type ID, $3 = cType 0=Input, E=1, I =2
	 cID = $1  tID = $2 cType = $3 }
objref syn_

proc synapses() {
  /* E0 */   soma syn_ = new Exp2Syn(0.5)  synlist.append(syn_)
    syn_.tau1 = EPSP_TAU1 //EPSP_TAU1 //1
    syn_.tau2 = EPSP_TAU2 //EPSP_TAU2 //3
	syn_.e = EPSP_E_REVERSE //EPSP_E_REVERSE//0
  /* I1 */   soma syn_ = new Exp2Syn(0.5)  synlist.append(syn_)
    syn_.tau1 = IPSP_TAU1 //IPSP_TAU1 //1
    syn_.tau2 = IPSP_TAU2 //IPSP_TAU2 //7
    syn_.e = IPSP_I_REVERSE //IPSP_I_REVERSE //-80 
}
func is_art() { return 0 }

endtemplate Target_Cell //////////////////////////////////////////////////////////End Target_Cell template  

begintemplate In_spk_VecStim ////////////////////////////////////////////////////////////////Begin In_spk_VecStim
public pp, connect2target, x, y, z, cID,tID,cType, position, is_art, setID
external NETCON_THRESHOLD  
objref pp
proc init() {  
  pp = new VecStim() 

}
func is_art() { return 1 } //artificial
obfunc connect2target() { localobj nc
  nc = new NetCon(pp, $o1)
  if (numarg() == 2) { $o2 = nc }
  nc.threshold = NETCON_THRESHOLD  
  return nc
}
proc position(){x=$1  y=$2  z=$3}
proc setID(){ cID = $1 tID = $2  cType = $3 }

endtemplate In_spk_VecStim  //////////////////////////////////////////////////////////////// End In_spk_VecStim 




printf("Done Downloading Cells Template")