load_file("nrngui.hoc")
load_file("olm-t.hoc")
cvode_active(1)

numaxon=1
numsoma=2
numbasal=52
numapical=70
numtrunk=49

xopen("geo5038804-S.hoc")             // geometry file
xopen("fixnseg.hoc")           

Rm = 28000
RmDend = Rm
RmSoma = Rm
RmAx = Rm

Cm    = 1
CmSoma= Cm
CmAx  = Cm
CmDend = Cm

RaAll= 150
RaSoma=150  
RaAx = 50

Vrest = -65
dt = 0.1
gna =  .025
fact=0.053431452
gkm=0.0015
gkmINT=0.0015
AXONM = 5
gkdr = 0.01
celsius = 35.0  
KMULT =  0.03
KMULTP = 0.03
ghd=0.00005
factor=1
norm=1
mod=0

objref  syni, syn1, syn0, nsi, ns1, ns0, nci, nc1, nc0, inter, apc, b, g, c

inter = new OLM()  

forsec "axon" {insert pas e_pas=Vrest g_pas = 1/RmAx Ra=RaAx cm=CmAx}
forsec "soma" {insert pas e_pas=Vrest g_pas = 1/RmSoma Ra=RaSoma cm=CmSoma}
forsec "dendrite"{insert pas e_pas=Vrest g_pas = 1/RmDend Ra=RaAll cm=CmDend}
forsec "user5" {insert pas e_pas=Vrest g_pas = 1/RmDend Ra=RaAll cm=CmDend}

access soma

freq=50
geom_nseg()
tot=0
forall {tot=tot+nseg}
distance()

soma apc   = new APCount(0.5)   
apc.thresh = -20.  

tstop=1100


b = new VBox()
b.intercept(1)
g = new Graph()
g.size(0,tstop,-80,40)
g.addvar("inter.inta[0].v(0.5)",3,1,2*tstop,0,2)
g.label(0.3,0.1,"int (Kv7.2+Kv7.3)")
g.exec_menu("10% Zoom out")
g.xaxis(1)
c = new Graph()
c.size(0,tstop,-80,40)
c.xaxis(1)
c.addvar("soma[0].v(0.5)",2,1,2*tstop,0,2)
c.exec_menu("10% Zoom out")
c.label(0.3,0.1,"CA1 (Kv7.2+Kv7.3)")	
b.intercept(0)
b.map()

user5[6]{
//  excitatory synapse
syn1= new Exp2Syn(.5)
syn1.tau1=0.5
syn1.tau2=5
syn1.e=0

ns1 = new NetStim(.5)
ns1.start=100
ns1.number=50
ns1.interval=20
ns1.noise=0.5
ns1.seed(225345)
}
nc1 = new NetCon(ns1, syn1, 0, 0, 0.039)


inter.inta[0]{
//excitatory synapse on BC
		syni= new Exp2Syn(.5) 
		syni.tau1=0.5
		syni.tau2=5
		syni.e=0
	}

nci = new NetCon(ns1, syni, 0, 3, 0.022)

soma[0]{
// inhibitory synapse 
syn0= new Exp2Syn(.5) 
syn0.tau1=0.5
syn0.tau2=7
syn0.e=-80 
}


inter.inta[0] nc0 = new NetCon(&v(.5), syn0, 0, 3, 0.15)

forsec "inta" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkmINT*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkmINT*mod
}

forsec "axon" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkm*3*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkm*3*mod
				insert nax gbar_nax=gna * AXONM
                insert kdr gkdrbar_kdr=gkdr
                insert kap gkabar_kap = KMULTP
}

forsec "soma" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkm*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkm*mod
		insert hd ghdbar_hd=ghd	vhalfl_hd=-73
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
                insert kap gkabar_kap = KMULTP
}

for i=0, numbasal-1 dendrite[i] {
		insert hd ghdbar_hd=ghd vhalfl_hd=-73
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
		insert kap gkabar_kap=0
		insert kad gkabar_kad=0

		for (x) if (x>0 && x<1) { xdist = distance(x)
                	ghdbar_hd(x) = ghd*(1+3*xdist/100)
                		if (xdist > 100){
					vhalfl_hd=-81
                        		gkabar_kad(x) = KMULT*(1+xdist/100)
                			} else {
					vhalfl_hd=-73
                        		gkabar_kap(x) = KMULTP*(1+xdist/100)
               				}
		}
}
                
forsec "apical_dendrite" {
			insert hd ghdbar_hd=ghd
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
		insert kap gkabar_kap=0
		insert kad gkabar_kad=0

		for (x) if (x>0 && x<1) { xdist = distance(x)
                	ghdbar_hd(x) = ghd*(1+3*xdist/100)
                		if (xdist > 100){
					vhalfl_hd=-81
                        		gkabar_kad(x) = KMULT*(1+xdist/100)
                			} else {
					vhalfl_hd=-73
                        		gkabar_kap(x) = KMULTP*(1+xdist/100)
							}
		}
}

forsec "user5" {
			insert hd ghdbar_hd=ghd
                insert na3 gbar_na3=gna
                insert kdr gkdrbar_kdr=gkdr
		insert kap gkabar_kap=0
		insert kad gkabar_kad=0

		for (x) if (x>0 && x<1) { xdist = distance(x)
                	ghdbar_hd(x) = ghd*(1+3*xdist/100)
                		if (xdist > 100){
					vhalfl_hd=-81
                        		gkabar_kad(x) = KMULT*(1+xdist/100)
								} else {
					vhalfl_hd=-73
                        		gkabar_kap(x) = KMULTP*(1+xdist/100)
               				}
		}
}

proc init() {
	t=0
        forall { 
        v=Vrest
        if (ismembrane("nax") || ismembrane("na3")) {ena=55}
        if (ismembrane("kdr") || ismembrane("kap") || ismembrane("kad")) {ek=-90}
       if (ismembrane("hd") ) {ehd_hd=-30}
	}
	finitialize(Vrest)
        fcurrent()

        forall { 
	for (x) {
	if (ismembrane("na3") || ismembrane("nax")){e_pas(x)=v(x)+(ina(x)+ik(x))/g_pas(x)}
	if (ismembrane("kv72wt73wt")|| ismembrane("kv72wt73wtR201C")){e_pas(x)=v(x)-norm*ik_kv72wt73wt(x)/g_pas(x)-mod*ik_kv72wt73wtR201C(x)/g_pas(x)}
//	if (ismembrane("kv72wt73wtR201C")){e_pas(x)=*v(x)-mod*ik_kv72wt73wtR201C(x)/g_pas(x)}
	if (ismembrane("hd")) {e_pas(x)=e_pas(x)+i_hd(x)/g_pas(x)}//
		}
	}
	cvode.re_init()
	cvode.event(tstop)
	access soma
	g.begin()
	c.begin()
}


proc advance() {
	fadvance()
	g.plot(t)
	g.flush() 
	c.plot(t)
	c.flush() 
	doNotify()	
	}

xpanel(" ")
xlabel("  ")
xradiobutton("Kv7.2+Kv7.3","setKv72()",1)
xradiobutton("Kv7.2+Kv7.2R201C+Kv7.3","setKvR201C()")
xlabel("  ")
xbutton("run", "run()")
xpanel()


proc setKv72() {
norm=1
mod=0
forsec "inta" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkmINT*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkmINT*mod}
forsec "axon" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkm*3*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkm*3*mod}
forsec "soma" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkm*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkm*mod}
g.erase_all()	
c.erase_all()
g.size(0,tstop,-80,40)
g.addvar("inter.inta[0].v(0.5)",3,1,2*tstop,0,2)
g.label(0.3,0.1,"int (Kv7.2+Kv7.3)")
g.exec_menu("10% Zoom out")
g.xaxis(1)
c.size(0,tstop,-80,40)
c.xaxis(1)
c.addvar("soma[0].v(0.5)",2,1,2*tstop,0,2)
c.exec_menu("10% Zoom out")
c.label(0.3,0.1,"CA1 (Kv7.2+Kv7.3)")
ns1.seed(225345)
run()	
}

proc setKvR201C() {
norm=0
mod=1
forsec "inta" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkmINT*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkmINT*mod}
forsec "axon" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkm*3*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkm*3*mod}
forsec "soma" {   
		insert kv72wt73wt gbar_kv72wt73wt=gkm*norm
		insert kv72wt73wtR201C gbar_kv72wt73wtR201C=gkm*mod}
g.erase_all()	
c.erase_all()
g.size(0,tstop,-80,40)
g.addvar("inter.inta[0].v(0.5)",4,1,2*tstop,0,2)
g.exec_menu("10% Zoom out")
g.xaxis(1)
c.size(0,tstop,-80,40)
c.xaxis(1)
c.addvar("soma[0].v(0.5)",1,1,2*tstop,0,2)
c.exec_menu("10% Zoom out")
g.label(0.3,0.1,"int (Kv7.2+Kv7.2R201C+Kv7.3)")
c.label(0.3,0.1,"CA1 (Kv7.2+Kv7.2R201C+Kv7.3)")	
ns1.seed(225345)
run()
}