// -------------------------------------------
// init active parameters for STDP simulations (Letzkus 2006)
// based on Schaefer (2003)
// uses mechanisms na, kv, ih, kca, km, kap, ca, cad
// ih distribution and nonunifrom resting Vm from Kole (2006)
//
// by B.Kampa 2006
// -------------------------------------------

proc init_params() {
		celsius=35
		Ri=125    
		Cm=1     
		Rm=15000 
		v_init=-89 	 		// JP corrected RMP under ZD 7288 
		spinescale=2.0  		// scale total area to account for spines 
		hfactor = 0.00042849 	// for 2.5 mM K+
		hinvtau = 0.0030776  	// for 2.5 mM K+
}


//-------------------------------------------------------------------------
// initalise passive properties and add spines by scaling Rm and Cm, NOT scaled for spines: axon, soma, 1st 100 um of apical dend, the 1st 20 um of basals 
//----------------------------------------------------------------------

proc init_spine() {
	forall {insert pas g_pas=1/(Rm/spinescale) cm=Cm*spinescale Ra=Ri 
}

axon {g_pas=0.02 }
soma {g_pas=1/Rm cm=Cm}

	 for i=0,14 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=15,27 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=28,32 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=43,47 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=71,72 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=777,778 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
 	for i=808,810 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=923,924 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=957,959 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=1006,1007 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=1055,1058 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=1059,1060 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
	 for i=1067,1069 {
		 dend1[i]  {g_pas=1/Rm cm=Cm}
	 }
}

//--------------------------------------------------
// initialise nonunifrom resting Vm
//----------------------------------------------------

proc init_vm() { 
	forall {
		e_pas=v_init
	}
	access dend1[21]
	distance()
	for i=0,1090 {
	dend1[i] {e_pas=v_init+(0*(distance(.5)))
		}
	}
}

// --------------------------------------------------------------------------
// Distribution exponential, now identical to patch data (Kole, Nov 2004)
// --------------------------------------------------------------------------

proc hexp() {
	forall {insert ih
			gamma_ih_ih=6.8e-13
			gh_ih = -0.0002
			} 
	
	access dend1[21]
	area(0.5)
	distance()
	printf("distributes Ih exponentially with dis from soma\n")
	for i=0,1090 {
	access dend1[i]
	ghdbar_ih=gh_ih+(hfactor*(exp(hinvtau*distance(.5))))
	ghdbar_ih=ghdbar_ih*spinescale	
	}


// the reconstructed axon 
	axon { ghdbar_ih=0 }
	soma { ghdbar_ih=0 }
	for i=0,13 {
		dend1[i] { ghdbar_ih=0 }
	}


// the real soma 
	for i=14,27 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}


// the 1st 100 um of the apical trunk 
	for i=28,32 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=43,47 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=71,72 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}


// the 1st 20 um of the basals 
	for i=777,778 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=808,810 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=923,924 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=957,959 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=1006,1007 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=1055,1058 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=1059,1060 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale }
	}
	for i=1067,1069 {
		dend1[i] { ghdbar_ih=ghdbar_ih/spinescale}
	
	}
}

// ----------------------------------------------------------
// all other active parameters
// ----------------------------------------------------------

proc init_active_params() {
	Ek = -90
	Ena = 60
	vshift_na = 3 //to get AP threshold of -63 mV (Gulledge & Stuart)

	gna_soma = 3000
	gkv_soma = 30
	gka_soma  = 0.06
	gkm_soma = 2.2
	gkca_soma = 2.5 
	git_soma = 0.0003
	gca_soma = 0

	gna_axon = 30000
	gkv_axon = 400

	gna_dend = 40 *spinescale
	gkv_dend = 30 *spinescale

	gka_dend  = 0.03 *spinescale
	gkm_dend = 0.05 *spinescale
	gkca_dend= 2.5 *spinescale
	gca_dend = 0 *spinescale
	git_dend = 0.0003 *spinescale

	gca_distal = 1.25 * spinescale
	git_distal = 0.001 *spinescale

	gka_slope = 0   // no gradient
	tauR   = 80
	caiExp = 4
	rA     = 0.05
	rB     = 0.1
}
proc init_active() {	

  // insert active properties
  // axon
  forall insert na
  forsec axon_only {insert na gbar_na = gna_axon}
  forsec axon_only {insert kv gbar_kv = gkv_axon}

  // dendrites
  forsec dendritic_only {
    insert na    gbar_na = gna_dend
    insert kv    gbar_kv = gkv_dend 
    insert km    gbar_km  = gkm_dend
    insert kca   gbar_kca = gkca_dend
    insert kap   gkabar_kap = gka_soma
    insert ca    gbar_ca = gca_dend
    insert it2   gcabar_it2=git_dend
    insert cad
  }

  // soma
  forsec somaAll {
    insert na    gbar_na = gna_soma
    insert kv    gbar_kv = gkv_soma
    insert km    gbar_km = gkm_soma
    insert kca   gbar_kca = gkca_soma
    insert kap   gkabar_kap = gka_soma
    insert ca    gbar_ca = gca_soma
    insert cad
  }

  forall if(ismembrane("k_ion")) ek = Ek
  forall if(ismembrane("na_ion")) {
    ena = Ena
  }
  forall if(ismembrane("ca_ion")) {
    eca = 140
    ion_style("ca_ion",0,1,0,0,0)
  }
}

// --------------------------------------------------------------
// Low Threshold Ca Channel to reproduce frequency effect (Larkum, Kaiser, Sakmann, PNAS,1999)
// --------------------------------------------------------------

proc boost_ca() {
	vh1_it2=56
	vh2_it2=415
	//ah_it2=30  // original value
	ah_it2=10	//changed to get faster Ca decay
	v12m_it2=45
	v12h_it2=65
	am_it2=3
	vshift_it2=0
	vm1_it2=50
	vm2_it2=125
	it2_init=0.0005
	gca_init=4.5
}

// --------------------------------------------------------------
// distal apical dendrites
// with slightly elevated Ca conductance densities
// --------------------------------------------------------------
proc InitDistal() {

	forsec dendritic_distal {
		gbar_ca = gca_distal
		gcabar_it2 = git_distal
	}
}

// --------------------------------------------------------------
// replace init procedure to have sufficiently long prepulse
// --------------------------------------------------------------

proc init() {local saveDt, i

  finitialize(v_init)
  fcurrent()
  saveDt = dt
  dt = 10
  for i=0,99 fadvance()
  dt = saveDt
print "new init"
}

proc DoIt() {local i, j

  init()
  for i=0,999 {
    for j=0,0.1/dt-1 fadvance()
  }
}