load_file("nrngui.hoc")
cvode.active(1)
cvode.atol(1e-3)

secondorder=2
FARADAY=96520
PI=3.14159

Vrest = -65
dt = 1

gna=0.01
gka=0.01
ghd=0.0001

colorg=1
tstop=200

objref nconp, ncond, net, netp, netd, c, g, b, synp, synd, nil
objref distrx, distry

create a
a {diam(0:1)=0.3:0.05 L=500 nseg=25}
a {
insert pas g_pas = 1/28000 Ra=150 cm=1 
insert ds
insert hd vhalfl_hd=-82
}


b = new VBox()
b.intercept(1)
g = new Graph()
g.size(0,tstop,-70,-50)
g.xaxis(1)
g.label(0.45,0.9,"time (ms)")
g.label(0.35,0.22,"S1")
g.mark(60,-66,"O",6,1,1)
g.exec_menu("10% Zoom out")
c = new Graph()
c.size(-60,60,4,12)
c.xaxis(1)
c.label(0.4, 0.02,"tS2 - tS1 (ms)")
c.label(0.4, 0.9,"peak depolarization")
c.exec_menu("10% Zoom out")
c.brush(2)
xpanel("",1)
xbutton("S1 and S2 prox (black)", "runp()")
xbutton("S1 prox, S2 dist (red)", "runm()")
xbutton("S1 and S2 dist (blue)", "rund()")
xbutton("S1 prox, S2 dist, +Ih (green)", "rundh()")
xpanel()
b.intercept(0)
b.map()

access a
	distance()
	netp = new NetStim(0)
	netp.number=1
	netd = new NetStim(0)
	netd.number=1

print distance(0)
	synp = new Exp2Syn(0)
	synp.e=0
	synp.tau1=3
	synp.tau2=3
	

print distance(0)
	synd = new Exp2Syn(0)
	synd.e=0
	synd.tau1=3
	synd.tau2=3

	nconp= new NetCon(netp,synp,0.5,0,0) 
	ncond= new NetCon(netd,synd,0.5,0,0) 

proc init() {
	t=0
        forall {v=Vrest ehd_hd=-30}
	for (x) {
		if (x*L<500) {xdist=x*L} else {xdist=500}
		ghdbar_hd(x)=ghd*(1+3*xdist/100)
	}

	finitialize(v)
        fcurrent()
        forall {
	for (x) {e_pas(x)=v(x)+i_hd(x)/g_pas(x)}
	}
	cvode.re_init()
	g.begin()
	g.plot(t)
}

proc step() {
	fadvance()
	g.plot(t)
}

proc run() {
	init()
	while(t<tstop) { step()}
	g.flush()
	doNotify()
}


proc runp() {
	c.begin()
	c.exec_menu("Keep Lines")
	distrx=new Vector()
	distry=new Vector()
	ghd=0.0
	synp.loc(0)
	synd.loc(0)
	nconp.weight=15e-6
	ncond.weight=15e-6
	color=1
	loop()
}

proc runm() {
	c.begin()
	c.exec_menu("Keep Lines")
	distrx=new Vector()
	distry=new Vector()
	ghd=0.0
	synp.loc(0)
	synd.loc(1)
	nconp.weight=15e-6
	ncond.weight=150e-6
	color=2
	loop()
}

proc rund() {
	c.begin()
	c.exec_menu("Keep Lines")
	distrx=new Vector()
	distry=new Vector()
	ghd=0.0
	synp.loc(1)
	synd.loc(1)
	nconp.weight=150e-6
	ncond.weight=150e-6
	color=3
	loop()
}

proc rundh() {
	c.begin()
	c.exec_menu("Keep Lines")
	distrx=new Vector()
	distry=new Vector()
	ghd=0.0001
	synp.loc(0)
	synd.loc(1)
	nconp.weight=15e-6
	ncond.weight=1200e-6
	color=4
	loop()
}

proc loop() {
	c.color(color)
	g.addvar("a.v(0)",color,1, 2*tstop,0,2)
	netd.start=3
	netp.start=60
		while (netd.start<120) {
//		if (netd.start>netp.start) {tstop=netd.start+50} else {tstop=netp.start+50}
		run()
		distrx.append(netd.start-netp.start)
		distry.append(vmax_ds(0)-Vrest)
		netd.start=netd.start+3
	}
		c.beginline()
		for index=0, distrx.size()-1 {
			c.line(distrx.x[index],distry.x[index])
			}
		c.flush()
		doNotify()
}