begintemplate model_0603_cell08_cm045

public init, biophys, geom_nseg, delete_axon,delete_spines,add_few_spines,active_biophys
public create_axon,change_cm,change_cm_rm

public soma, dend, apic, axon,spine
public all, somatic, apical, axonal, basal,Spines
objref all, somatic, apical, axonal, basal,Spines, this

strdef tstr

proc init() {
	all = new SectionList()
	somatic = new SectionList()
	basal = new SectionList()
	apical = new SectionList()
	axonal = new SectionList()
	Spines = new SectionList()
	
 	forall delete_section()
 	StepDist = 60 // Almost no spines in human cells within the first 60 um
 				  // from soma - see Benavides-Piccione 2013
	F_Spines = 1.9       //As calculated - see detailes in Eyal 2015
	//Results of the fitting algorithm
	CM =0.45234   	// uF/cm2
	RM = 38907		// Ohm-cm2	
	RA = 203.23 	// Ohm-cm
	// Junction Potential of 16 mV. Not important anyway for the passive model 
	// But important when adding voltage dependant process - synapses and VG ion channels
	E_PAS =  -86
	celsius = 37
	NA_BAR = 8000
	THA_NA = -43
	K_BAR = 3200
	EK = -90
	AX_NA_BAR = 200
	AX_K_BAR = 100
	THA_KV = 25
}

create soma[1], dend[1], apic[1], axon[1],spine[1]

external lambda_f
proc geom_nseg() {

  forsec all {

  nseg = 1 + 2*int(L/40)

  }

}


proc biophys() {
	
	
	forsec all {
	
	   insert pas
		
		cm =CM
	    g_pas=1/RM
		Ra = RA
		e_pas = E_PAS
	}
	
	soma distance()
	
	forsec basal {	
	  for (x){
			if (distance(x)>StepDist) {
				cm(x)=CM*F_Spines
				g_pas(x)=(1/RM)*F_Spines
			}	
		}
	}
	forsec apical {
	  for (x){
			if (distance(x)>StepDist) {
				cm(x)=CM*F_Spines
				g_pas(x)=(1/RM)*F_Spines
				

			}
		}	
	}


	
}

proc active_biophys(){
	soma{
		insert na
		insert kv
		tha_na = THA_NA
		tha_kv = THA_KV
		gbar_na =  NA_BAR
		gbar_kv = K_BAR
		ek = EK
	}
	forsec axonal{
		insert na
		insert kv
		tha_na = THA_NA
		tha_kv = THA_KV
		gbar_na =  AX_NA_BAR
		gbar_kv = AX_K_BAR
		ek = EK
	}
}


proc delete_axon(){
    forsec axonal{delete_section()}

}

proc create_axon(){
	L1 = 1000
	L2 = 5000
	create axon[2]
	d = 1/10^4
	

	access axon[0]
	diam = d*10^4
	L = L1
	nseg = 201
	axonal.append()
	all.append()

	access axon[1]
	diam = d*10^4
	L = L2
	nseg = 201
	axonal.append()
	all.append()
	connect axon[0](0), soma(1)
	connect axon[1](0), axon[0](1)
	access soma


	


}

// allows to delete all spines in the model
proc delete_spines(){
	forsec Spines{delete_section()}
}

// adding spines in the locations defined in sref_list in segment x_vec
proc add_few_spines(){localobj sref_list, x_vec,sref
	PI = 3.14159265359  
	sref_list = $o1
	x_vec = $o2
	neck_diam = $3
	neck_len = $4
	spine_head_area = $5
	ra = $6


	L_head = 2*sqrt(spine_head_area /4/PI) //sphere has the same surface area as cylinder with L=diam
											//note that neorun don't include the bottom and the up of a cylinder in the area
											// so 2*pi*r*h = pi*diam*Length = pi*diam^2 = pi*4*r^2
	diam_head = L_head
	create spine[2*sref_list.count()]
	for (j=0;j<sref_list.count();j+=1){

		sref = sref_list.o(j)
		shaft_x  = x_vec.x[j]
		spine[2*(j)]{
			L = neck_len
			diam = neck_diam
			insert pas
			cm =CM
	    	g_pas=1/RM
			e_pas = E_PAS
			Ra = ra
			Spines.append()

		}
		spine[2*(j)+1]{
			L = L_head
			diam = diam_head
			insert pas
			cm =CM
	    	g_pas=1/RM
			e_pas = E_PAS
			Ra = ra
			Spines.append()

		}
		connect  spine[2*(j)+1](0) ,spine[2*(j)](1)
		sref.sec{ 
			connect spine[2*(j)](0), shaft_x
		}
	

	}
	
}

proc change_cm(){
	cm_factor = $1
	forsec all{
		cm =CM*cm_factor

	}
	forsec basal {	
	  for (x){
			if (distance(x)>StepDist) {
				cm(x)=CM*cm_factor*F_Spines
			}	
		}
	}
	forsec apical {
	  for (x){
			if (distance(x)>StepDist) {
				cm(x)=CM*cm_factor*F_Spines
				
			}
		}	
	}


}

proc change_cm_rm(){
	cm_factor = $1
	forsec all{
		cm =CM*cm_factor
		g_pas = (1/RM)*cm_factor

	}
	forsec basal {	
	  for (x){
			if (distance(x)>StepDist) {
				cm(x)=CM*cm_factor*F_Spines
				g_pas(x) = (1/RM)*cm_factor*F_Spines
			}	
		}
	}
	forsec apical {
	  for (x){
			if (distance(x)>StepDist) {
				cm(x)=CM*cm_factor*F_Spines
				g_pas(x) = (1/RM)*cm_factor*F_Spines
				
			}
		}	
	}


}






endtemplate model_0603_cell08_cm045