// Artificial cells no longer need a default section.
//Network cell templates
//   VIPCell
// Simplified version (BPG 27-9-08)
//  - geometry and channels from Santhakumar et al 2005
//  - geometry modified to preserve VCUs different dendrites

begintemplate VIPCRCell
public is_art
public init, topol, basic_shape, subsets, geom, biophys
public pre_list, connect2target

public soma
public radProx1, radMed1, radDist1, lmM1, lmt1
public radProx2, radMed2, radDist2, lmM2, lmt2
public oriProx1, oriMed1, oriDist1
public oriProx2, oriMed2, oriDist2
public all

objref pre_list

proc init() {
	topol()
	subsets()
	geom()
	biophys()
	geom_nseg()
	pre_list = new List()
	synapses()
}

create soma
create radProx1, radMed1, radDist1, lmM1, lmt1
create radProx2, radMed2, radDist2, lmM2, lmt2
create oriProx1, oriMed1, oriDist1
create oriProx2, oriMed2, oriDist2

proc topol() { local i
	connect radProx1(0), soma(0)
	connect radMed1(0),  radProx1(1)
	connect radDist1(0), radMed1(1)
	connect lmM1(0),     radDist1(1)
	connect lmt1(0),     lmM1(1)
	connect radProx2(0), soma(1)
	connect radMed2(0),  radProx2(1)
	connect radDist2(0), radMed2(1)
	connect lmM2(0),     radDist2(1)
	connect lmt2(0),     lmM2(1)	
	connect oriProx1(0), soma(0)
	connect oriMed1(0),  oriProx1(1)
	connect oriDist1(0), oriMed1(1)
	connect oriProx2(0), soma(1)
	connect oriMed2(0),  oriProx2(1)
	connect oriDist2(0), oriMed2(1)
	//basic_shape()
}

proc basic_shape() {
	soma     { pt3dclear() pt3dadd(0, 0, 0, 10)      pt3dadd(15, 0, 0, 10)        }
	radProx1 { pt3dclear() pt3dadd(0, 0, 0, 4)       pt3dadd(-7, 7.50, 0, 4)      }
	radMed1  { pt3dclear() pt3dadd(-14, 15, 0, 3)    pt3dadd(-21.5, 22.5, 0, 3)   }
	radDist1 { pt3dclear() pt3dadd(-29, 30, 0, 2)    pt3dadd(-36.5, 37.5, 0, 2)   }
	lmM1     { pt3dclear() pt3dadd(-44, 45, 0, 1.5)  pt3dadd(-51.5, 52.5, 0, 1.5) }
	lmt1     { pt3dclear() pt3dadd(-59, 60, 0, 1)    pt3dadd(-74, 75, 0, 1)       }
	radProx2 { pt3dclear() pt3dadd(15, 0, 0, 4)      pt3dadd(30, 15, 0, 4)        }
	radMed2  { pt3dclear() pt3dadd(45, 30, 0, 3)     pt3dadd(60, 45, 0, 3)        }
	radDist2 { pt3dclear() pt3dadd(75, 60, 0, 2)     pt3dadd(82.5, 67.5, 0, 2)    }
	lmM2     { pt3dclear() pt3dadd(90, 75, 0, 1.5)   pt3dadd(97.5, 82.5, 0, 1.5)  }
	lmt2     { pt3dclear() pt3dadd(105, 90, 0, 1)    pt3dadd(112.50, 97.5, 0, 1)  }	
	oriProx1 { pt3dclear() pt3dadd(0, 0, 0, 2)       pt3dadd(-14.5, -14.5, 0, 2)  }
	oriMed1  { pt3dclear() pt3dadd(-29, -29, 0, 1.5) pt3dadd(-44, -44, 0, 1.5)    }
	oriDist1 { pt3dclear() pt3dadd(-59, -59, 0, 1)   pt3dadd(-74, -74, 0, 1)      }
	oriProx2 { pt3dclear() pt3dadd(15, 0, 0, 2)      pt3dadd(30, -14.5, 0, 2)     }
	oriMed2  { pt3dclear() pt3dadd(45, -29, 0, 1.5)  pt3dadd(60, -44, 0, 1.5)     }
	oriDist2 { pt3dclear() pt3dadd(75, -59, 0, 1)    pt3dadd(90, -74, 0, 1)       }
}

objref all
proc subsets() { local i
	objref all
	all = new SectionList()
	soma     all.append()
	radProx1 all.append()
	radMed1  all.append()
	radDist1 all.append()
	lmM1     all.append()
	lmt1     all.append()
	radProx2 all.append()
	radMed2  all.append()
	radDist2 all.append()
	lmM2     all.append()
	lmt2     all.append()	
	oriProx1 all.append()
	oriMed1  all.append()
	oriDist1 all.append()
	oriProx2 all.append()
	oriMed2  all.append()
	oriDist2 all.append()
}

proc geom() {
	forsec all {  }
	soma     {  L = 20   diam = 10  }
	radProx1 {  L = 50   diam = 4   }
	radMed1  {  L = 50   diam = 3   }
	radDist1 {  L = 100  diam = 2   }
	lmM1     {  L = 50   diam = 1.5 }
	lmt1     {  L = 50   diam = 1   }
	radProx2 {  L = 50   diam = 4   }
	radMed2  {  L = 50   diam = 3   }
	radDist2 {  L = 100  diam = 2   }
	lmM2     {  L = 50   diam = 1.5 }
	lmt2     {  L = 50   diam = 1   }	
	oriProx1 {  L = 50   diam = 2   }
	oriMed1  {  L = 50   diam = 1.5 }
	oriDist1 {  L = 50   diam = 1   }
	oriProx2 {  L = 50   diam = 2   }
	oriMed2  {  L = 50   diam = 1.5 }
	oriDist2 {  L = 50   diam = 1   }
}

external lambda_f
proc geom_nseg() {
  forsec all { nseg = int((L/(0.1*lambda_f(100))+.9)/2)*2 + 1  }
}

proc biophys() {

	soma_nafcr   = 0.015
	soma_kdrcr   = 0.018 
	soma_Kslowcr = 0.000725
	soma_iCcr    = 0.00003
	soma_kadcr   = 0.003
	soma_cancr   = 0.001
	v_initcr     = -70
	
	soma {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr= soma_nafcr

		insert kdrcr
		gkdrbar_kdrcr= soma_kdrcr

		insert IKscr
		gKsbar_IKscr= soma_Kslowcr

		insert iCcr
		gkcbar_iCcr= soma_iCcr

		insert kadcr
		gkabar_kadcr= soma_kadcr

		insert cancr
		gcatbar_cancr=soma_cancr

		insert cadyn
		// TO DO: cadyn_new, compile,insert, and verify the validation tests
	}
	
	radProx1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}

	radProx2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}	

	radMed1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		

	radMed2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}

	radDist1 {
		insert pas
		cm        = 1.2            //microF/cm2
		g_pas     = 1/20000
		e_pas     = v_initcr
		v_initcr  = -70
		Ra        = 150

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		

	radDist2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		

	lmM1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		

	lmM2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}

	lmt1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		

	lmt2 {
		insert pas
		cm        = 1.2            //microF/cm2
		g_pas     = 1/20000
		e_pas     = v_initcr
		v_initcr  = -70
		Ra        = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}					

	oriProx1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}			

	oriProx2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}			

	oriMed1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		

	oriMed2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}		
	
	oriDist1 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}			

	oriDist2 {
		insert pas
		cm       = 1.2            //microF/cm2
		g_pas    = 1/20000
		e_pas    = v_initcr
		v_initcr = -70
		Ra       = 150 

		insert Nafcr
		gnafbar_Nafcr = 0.018*5

		insert kdrcr
		gkdrbar_kdrcr = 0.018*0.5
	}			

	forsec all {
		ko0_k_ion = 3.82   //mM
	 	ki0_k_ion = 140    //mM
		celsius   = 23
	} 					

}

obfunc connect2target() { localobj nc //$o1 target point process, optional $o2 returned NetCon
	soma nc = new NetCon(&v(1), $o1)
	nc.threshold = -10
	if (numarg() == 2) { $o2 = nc } // for backward compatibility
	return nc
}

objref syn_

proc synapses_EC() {
	/* E0 */   lmM1 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		EC
	syn_.tau1 = 2.0
	syn_.tau2 = 6.3
	syn_.e    = 0
	/* E1 */   lmM2 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		EC
	syn_.tau1 = 2.0
	syn_.tau2 = 6.3
	syn_.e    = 0
}

proc synapses_CA3() {
	/* E2 */   radMed1 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		CA3 Shaffer collateral
	syn_.tau1 = 2.0
	syn_.tau2 = 6.3
	syn_.e    = 0
	/* E3 */   radMed2 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		CA3 Shaffer collateral
	syn_.tau1 = 2.0
	syn_.tau2 = 6.3
	syn_.e    = 0
	/* E4 */   radProx1 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		CA3 Shaffer collateral
	syn_.tau1 = 2.0
	syn_.tau2 = 6.3
	syn_.e    = 0
	/* E5 */   radProx2 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		CA3 Shaffer collateral
	syn_.tau1 = 2.0
	syn_.tau2 = 6.3
	syn_.e    = 0
}

proc synapses_PC() {
	/* E6 */   oriProx1 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		PC
	syn_.tau1 = 0.5
	syn_.tau2 = 3.0
	syn_.e    = 0
	/* E7 */   oriProx2 syn_ = new MyExp2Syn(0.5)  pre_list.append(syn_)	// AMPA		PC
	syn_.tau1 = 0.5
	syn_.tau2 = 3.0
	syn_.e    = 0
}

proc synapses_SEP() {
	/* I8 */   oriProx1 syn_ = new MyExp2Syn(0.6)  pre_list.append(syn_)	// GABA-A	Septum
	syn_.tau1 = 1
	syn_.tau2 = 8
	syn_.e    = -75
	/* I9 */   oriProx2 syn_ = new MyExp2Syn(0.6)  pre_list.append(syn_)	// GABA-A	Septum
	syn_.tau1 = 1
	syn_.tau2 = 8
	syn_.e    = -75
	/* I10 */   oriProx1 syn_ = new MyExp2Syn(0.6)  pre_list.append(syn_)	// GABA-B	Septum
	syn_.tau1 = 35
	syn_.tau2 = 100
	syn_.e    = -75
	/* I11 */   oriProx2 syn_ = new MyExp2Syn(0.6)  pre_list.append(syn_)	// GABA-B	Septum
	syn_.tau1 = 35
	syn_.tau2 = 100
	syn_.e    = -75
}


proc synapses() {
  synapses_EC()
  synapses_CA3()
  synapses_PC()
  synapses_SEP()
}



func is_art() { return 0 }

endtemplate VIPCRCell