//---------------------------------------------------------------------------------------------------------------------------------//
// Localization of intracellular stimulation at the soma and Ca-PIC channels over dendrites at the similar distance from the soma  //					
//---------------------------------------------------------------------------------------------------------------------------------//

// global constants
v_init=-70
num_row=10000										

// global variables
objref iCaL[num_row]
objref istim

// localization of Ca-PIC channels at the similar Dpath
proc loc_dend(){local max_dist, err_min, dpath, init, end, err_dist, x_min, j 

	max_dist=0											
	err_min=0
	init=0										
	end=0											
	err_dist=0										
	x_min=0
	j=0

	// calculate the longest Dpath from the soma
	forall {
		soma distance(0, 0)							
		for(x){
			if (max_dist < distance(x)) {					
			max_dist = distance(x)
			}
		}
		err_min=max_dist								
	}
	
	// set the specific Dpath
	dpath = 800 											 
  	forall {
		init = distance(0)				     
		end  = distance(1)				     
		if (init<=dpath && dpath<end) {				 
			for(x) {
				err_dist=abs(dpath-distance(x))	 
				if (err_min > err_dist) {	 
					err_min = err_dist
					x_min=x
				}	
			}
			if(x_min==0){x_min=0.0001}
			if(x_min==1){x_min=0.9999}
			
			iCaL[j] = new CaL(x_min)
			//iCaL[j].gcalbar=1.57 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 100um 
			//iCaL[j].gcalbar=1.14 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 200um 
			//iCaL[j].gcalbar=1.21 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 300um 
			//iCaL[j].gcalbar=1.25 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 400um
			//iCaL[j].gcalbar=1.28 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 500um
			//iCaL[j].gcalbar=1.37 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 600um
			//iCaL[j].gcalbar=1.39 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 700um
			iCaL[j].gcalbar=1.95 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 800um
			//iCaL[j].gcalbar=2.8 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 900um
			//iCaL[j].gcalbar=4.1 * area(x_min) * (10^-4)^2 * 10^3 //[mS/cm2] for dpath = 1000um
			
			j=j+1						 
			err_min=max_dist			  
		}
	}
}

loc_dend()

// Intracellular current injection at the soma
access soma
istim = new SawtoothIClamp(0.5)
istim.tp = 2000
istim.pkamp = 15
istim.bias = -5