load_file("stdlib.hoc")
load_file("ObliquePath.hoc")

objref cvode
cvode = new CVode()
cvode.active(0)


create axon[2]
objref SD, AXON, SA, Basal, Trunk, AIS
objref LM, RAD, RADt, LUC, PSA, PSB, ORI
create soma[1], apical[1], basal[1]
objref pl[150], opl[150]

objref netlist, s, ampasyn, f1, DEND, sapamp, somavec, sampvec
strdef  str2



dt = 0.025
tstop = 1650// 1050 //1sec (first 50ms not counted)
steps_per_ms=40

Rm_soma=80000
Rm_end=400
Rm_dend = Rm_soma
Rm_axon = Rm_soma
rm_xhalf = 225
rm_slope = 30

Ra_soma=150
Ra_end=150
Ra_axon = Ra_soma
Ra_xhalf = 210
Ra_slope = 50


c_m       = 1
cmsoma    = c_m
cmdend    = c_m*1.8
cmaxon    = c_m

Vrest = -65
v_init = -65
celsius = 34.0


// uncomment/call find_epsp_amplitudes whenever u change any of the following parameters .. 

gna=0.02
gkdr=0.0014
gna=0 //this is the w/o Na file
gkdr=0 //this is the w/o kdr file

gexFac = 0.9

AISFactor=5
epsp_amp = 0.0025  //mV



objref ampa_filename
ampa_filename = new String()
gh=85e-06//µS set to the value for which you'll be running withH.hoc and WithHfast.hoc

ghmax=20
xhalf=250
slopegrad=50

sprint(ampa_filename.s,"g_dist_withgh0_epspAmp_%f_calculated.txt",epsp_amp)



/********************************************************************/


Ek = -90
Ena = 55
Eh=-30


/********************************************************************/
//radial distance calculation

somax=2.497
somay=-13.006
somaz=11.12
double distances[200]

func raddist() {
	distn0=distance(0)
	distances[0]=0
	sum=0
    
	for i=1,n3d()-1 {
		xx=(x3d(i)-x3d(i-1))*(x3d(i)-x3d(i-1))
		yy=(y3d(i)-y3d(i-1))*(y3d(i)-y3d(i-1))
		zz=(z3d(i)-z3d(i-1))*(z3d(i)-z3d(i-1))
		sum=sum+sqrt(xx+yy+zz)
		distances[i]=sum
	}
    
	xval=$1
    
    // Amoung the various pt3d's find which one matches the distance of
    //  x closely
    
	distn=distance(xval)
	match=distn-distn0
	matchptdist=100000
	for i=0,n3d()-1 {		
		matptdist=(match-distances[i])*(match-distances[i])
		if(matchptdist>matptdist){
			matchptdist=matptdist
			matchi=i
		}
	}
    
	//print "Match for ", x, " is ", matchi, " XDIST ", match, " MATCH ", distances[matchi], " ERROR ", sqrt(matchptdist)
    
    
    // Find the distance of the closely matched point to the somatic
    // centroid and use that as the distance for this BPAP measurement			
    
	xx=(x3d(matchi)-somax)*(x3d(matchi)-somax)
	yy=(y3d(matchi)-somay)*(y3d(matchi)-somay)
	zz=(z3d(matchi)-somaz)*(z3d(matchi)-somaz)
	return sqrt(xx+yy+zz)
}

/********************************************************************/

proc update_init(){
	finitialize(v_init)
	fcurrent()
    forall {
        for (x){
            if (ismembrane("hd")||ismembrane("nas")||ismembrane("na3")||ismembrane("nax")) {
                e_pas(x)=v(x)+(i_hd(x)+ina(x)+ik(x))/g_pas(x)
            } else {
                e_pas(x)=v(x)
            }
        }
    }
}

/**********************************************************************/
// Passive Conductances 

proc setpassive(){
    forall {
        insert pas
        e_pas = v_init
        Ra=Ra_soma
    }
    forsec SD{		// For somato-dendritic compartments
        cm=cmdend
        g_pas=1/Rm_dend
    }
    forsec "soma" {
        cm = cmsoma 
        g_pas=1/Rm_soma
    }
    forsec Trunk {
        for (x) {
            rdist=raddist(x)
            rm = Rm_soma + (Rm_end - Rm_soma)/(1.0 + exp((rm_xhalf-rdist)/rm_slope))
            Ra = Ra_soma + (Ra_end - Ra_soma)/(1.0 + exp((Ra_xhalf-rdist)/Ra_slope))
            g_pas(x)=1/rm
        }
    }	
    
    for i=0,plcount {
        seccount=0
        forsec pl[i] {
            if(!seccount){
                trunk_pas=g_pas(1)
                seccount=seccount+1
            } else {
                g_pas=trunk_pas
                seccount=seccount+1
            }
            //print secname() 
        }
    }       
    
}
/**********************************************************************/
// Active Conductances 



proc setactive () {
    
	forall{
        insert na3
        gbar_na3= gna 
        insert kdr 
        gkdrbar_kdr=gkdr
        
        
        insert hd  
		ghdbar_hd=gh vhalfl_hd=-82
        tfactor_hd=1
	}
    
	forsec AXON{
        gbar_na3= 0 
        gkdrbar_kdr=0
        ghdbar_hd=0
        
    }
    
    
    forsec "apical"{
        insert nas
        gbar_nas = gna //has a slow "s" factor
        
        gbar_na3 = 0 //since this was added forall, remove from apical section
        
    }
    
    forsec AIS {
        gbar_na3= 0 
        insert nax 
        gbar_nax = gna*AISFactor//0//10          //5             //50
        
        gkdrbar_kdr=gkdr
        ghdbar_hd=0
	}
    
    forall{
        if(ismembrane("hd")){
            ehd_hd=-30
        }
        
        if(ismembrane("na3") || ismembrane("nas") || ismembrane("nax")){
            ena=55
        }
        
        if(ismembrane("kdr")){
            ek=-90
        }
        
        
        
    }
    
    
	
}


/********************************************************************/
proc gh_gradient(){
	
	
	forsec Trunk {	// Trunk
        
		for (x) {
			xdist=raddist(x)
			
			ghdbar_hd(x) = gh*(1+ghmax/(1+exp(-(xdist-xhalf)/slopegrad)))             
            
			if (xdist > 100){
				if (xdist>300) { 
					ndist=300
				} else { // 100 <= xdist <= 300
					ndist=xdist
				}
				vhalfl_hd(x)=-82-8*(ndist-100)/200
			} else {	// xdist < 100
				vhalfl_hd(x)=-82
			}	
            
		}
	}
    
	for i=0,plcount { // Apical obliques
    	seccount=0
    	forsec pl[i] {
        	if(!seccount){	// The first section is the trunk
            	trunk_h=ghdbar_hd(1)
				trunk_vhalf=vhalfl_hd(1)
				seccount=seccount+1
        	} else {
				
            	ghdbar_hd=trunk_h
				vhalfl_hd=trunk_vhalf
				seccount=seccount+1
        	}
        	//print secname() 
    	}
	}       
    
	forsec "soma" {
        ghdbar_hd=gh vhalfl_hd=-82
	}	
    
	forsec Basal {
        ghdbar_hd=gh vhalfl_hd=-82
	}	
    
	forall if (ismembrane("hd") ) ehd_hd = Eh
        
        
        
        
        }


/**********************************************************************/
proc load_3dcell() {
    
    // $s1 filename
    
	forall delete_section()
	xopen($s1)
    
	access soma[2] //define origin for distance calculation
	distance()
    
    
	SD = new SectionList() //Somato-dendritic section
	SA = new SectionList() // Somato-axonic section
	Trunk = new SectionList() //Trunk
	Basal = new SectionList() //Basal
    
	forsec "soma" {
		SD.append()
		SA.append()
	}
    
	forsec "basal" { 
		SD.append()
		Basal.append()
	}
    
	forsec "apical"{
		SD.append()
		SA.append()
	}
    
	// Trunk. 
    
	soma[0]	   Trunk.append()
	apical[0]  Trunk.append() 
	apical[4]  Trunk.append() 
	apical[6]  Trunk.append() 
	apical[14] Trunk.append() 
	apical[15] Trunk.append() 
	apical[16] Trunk.append() 
	apical[22] Trunk.append() 
	apical[23] Trunk.append() 
	apical[25] Trunk.append() 
	apical[26] Trunk.append() 
	apical[27] Trunk.append() 
	apical[41] Trunk.append() 
	apical[42] Trunk.append() 
	apical[46] Trunk.append() 
	apical[48] Trunk.append() 
	apical[56] Trunk.append() 
	apical[58] Trunk.append() 
	apical[60] Trunk.append() 
	apical[62] Trunk.append() 
	apical[64] Trunk.append() 
	apical[65] Trunk.append() 
	apical[69] Trunk.append() 
	apical[71] Trunk.append()
	apical[81] Trunk.append() 
	apical[83] Trunk.append() 
	apical[95] Trunk.append()
	apical[103] Trunk.append()
	apical[104] Trunk.append()
    
    load_file("oblique-paths.hoc")
    
    setpassive() //before setting the nseg
    // The lambda constraint
    totcomp=0
    forall{
        nseg=int((L/(0.1*lambda_f(100))+0.9)/2)*2+1   
        totcomp=totcomp+nseg
    }
    print "totcomp = ",totcomp
    
    
    
	init_cell() //calls setpassive()
    
    DEND = new SectionList()
    forsec "apical"{
        xdist=raddist(1)
        if(xdist<300 && xdist > 50){
            DEND.append()
        }    
    }
    
    
    
	
}

/**********************************************************************/
// For cell number n123 on the DSArchive, converted with CVAPP to give
// HOC file, the following definition holds. This is the same as Poirazi et
// al. have used in Neuron, 2003. The argument is that the subtree seems
// so long to be a dendrite, and the cell does not have a specific axon.
// There is a catch, though, if the morphology is closely scanned, then 
// basal dendrites would branch from these axonal segments - which
// may be fine given the amount of ambiguity one has while tracing! 

proc addaxon() {
    
	AXON = new SectionList()
    AIS = new SectionList() // Axonal initial segment
    
	for i = 30,34 basal[i] {
		AXON.append()
		Basal.remove()
	}
    
	for i = 18,22 basal[i] {
		AXON.append()
        AIS.append()
		Basal.remove()
	}
    
	forsec AXON {
		e_pas=v_init 
		g_pas = 1/Rm_axon 
		Ra=Ra_axon 
		cm=cmaxon
	}
}

/********************************************************************/





proc init_cell() {
	setpassive()
	addaxon()
	setactive()
	gh_gradient() //sets the gh gradient
	
	access soma[2]	// Reinitializing distance origin
	distance()
    
	finitialize(v_init)
	fcurrent()
    forall {
        for (x) {
            if (ismembrane("hd")||ismembrane("nas")||ismembrane("na3")||ismembrane("nax")) {
                e_pas(x)=v(x)+(i_hd(x)+ina(x)+ik(x))/g_pas(x)
            } else {
                e_pas(x)=v(x)
            }
        }
    }   
}


/********************************************************************/


load_3dcell("n123.hoc") //calls setpassive()


/********************************************************************/


///****************************************************************/


objref ZAPStim,fzap
ZAPAmp=0.01

proc GenerateZAP() {
	//ZAPStim=new Vector(1050/dt)
	ZAPStim=new Vector(25050/dt)
	//if(ZAP_DONE==0) {
	//	ZAPStim=new Vector(25050/dt)
	//	ZAPStim=new Vector(150050/dt)
	//	ZAP_DONE=1
	//}	
	ZAPStim.fill(0)
	for i=50/dt, ZAPStim.size()-1 {
		xval=(i-50/dt)*dt/1000
        //	ZAPStim.x[i]=ZAPAmp*sin(2*3.141592654*(1/log(150^(1/25)))*(((150^(1/25))^xval-1))) //Exponential chirp
        //	ZAPStim.x[i]=ZAPAmp*sin(2*3.141592654*(0.1/log(1500^(1/25)))*(((1500^(1/25))^xval-1))) //Exponential chirp
        //	ZAPStim.x[i]=ZAPAmp*sin(2*3.141592654*(0.1/log(1500^(1/20)))*(((1500^(1/20))^xval-1))) //Exponential chirp
		
		//ZAPStim.x[i]=ZAPAmp*sin(2*3.141592654*150*(i-50/dt)/(2*(150000/dt))*xval)// Linear chirp
        //	ZAPStim.x[i]=ZAPAmp*sin(2*3.141592654*8*xval) // Pure sinusoid
		ZAPStim.x[i]=ZAPAmp*sin(2*3.141592654*25*(i-50/dt)/(2*(25000/dt)) *xval)
	}
	fzap=new File()
	fzap.wopen("zap.txt")
	ZAPStim.printf(fzap,"%f\n")
}

objref f_name,f2,v4
f_name= new String()
v3=new Vector()
v4=new Vector()

proc ZAP_Trunk() {
    GenerateZAP()
    cvode.active(0)
    print "ZAP Generated....\n"
    count=0
    tstop=25050	//25050
    //tstop=25
    //tstop=150050
    
    update_init()

    forsec Trunk{

		for(x) {
			//if((x != 0) && (x<1)){ // x=0 distance is equal to x=1 of prev section
            if(x==0.5){
				print secname(), " ", x, " ", raddist(x)
				
                
                st=new IClamp(x)
                st.dur=tstop
                st.del=0
                
                v3.record(&v(x)) //local
                v4.record(&soma.v(0.5)) //transfer


                f1=new File()
                sprint(f_name.s,"Zap_%s.txt",secname())
                print f_name.s
                
                f1.wopen(f_name.s)

                f2=new File()
                sprint(f_name.s,"Zap_Trans_%s.txt",secname())
                print f_name.s

                f2.wopen(f_name.s)

                ZAPStim.play(&st.amp,dt)

                finitialize(v_init)
                fcurrent()
                
                while (t < tstop){
                    fadvance()
                    
                }
                v3.printf(f1)
                f1.close()
                v4.printf(f2)
                f2.close()
            }
            
        }
    }
}



ZAP_Trunk()