strdef cmd
objref volt_cl			  			
objref vbox1, vbox2, vbox3, vbox4, vbox5		
objref g_v, g_i1, g_i2, g_c 			
objref v_vec, nctr_vec, tr_vec
objref ms			

v_vec = new Vector()
nctr_vec = new Vector()
tr_vec = new Vector()

y_min = -1.0

create soma
soma {
	diam = 21.851        
	L    = 21.851 	
	nseg = 1 	     
	cm   = 1        
	Ra   = 60       
	insert CaR 
	cai0_ca_ion = 0.000001
	cao0_ca_ion = 4
	celsius = 22
	volt_cl = new VClamp_plus(.5)
}

vbox5 = new VBox()
vbox5.intercept(1)
ms = new MechanismStandard("CaR")
ms.action("change_CaR()")
ms.in()
ms.panel()
vbox5.intercept(0)
vbox5.map("CaR Control", 1, 854, -1, -1)


proc change_CaR() {
	ms.out()
}


proc Clamp() {

     volt_cl.amp[1]=$1	   
     volt_cl.amp[0]=hold_pot
     volt_cl.amp[2]=hold_pot

     soma_curr_tr=0        

     finitialize(v_init)

     pre_i = 0

     while (t<tstop) {

             dens=soma.ica(.5) 
             g_v.line(t, soma.v(.5))
             g_v.flush()
             g_i1.line(t, dens)
             g_i1.flush()

             if ((t>volt_cl.dur[0]) && (t<(volt_cl.dur[0]+volt_cl.dur[1]))) {
             	
                if (abs(dens)>abs(pre_i)) {


             		execute(cmd)
                	soma_curr_tr=dens
            	}
             }
 
         	fadvance()
         	pre_i=dens
     }

     v_vec.append($1)
     nctr_vec.append(soma_curr_tr)
     tr_vec.append(soma_curr_tr)

     doEvents()
}


proc erase() {

    g_v.erase(0)
    g_i1.erase(0)
    g_v.size(0, tstop, hold_pot-10, end_cl+10)
    g_i1.size(0, tstop, y_min, 0.1)
    g_v.beginline()
    g_i1.beginline(2,1)

}

proc start() {
	
    v_vec.resize(0)
    nctr_vec.resize(0)
    tr_vec.resize(0)
    g_i2.erase(0)
    g_i2.size(st_cl, end_cl, y_min, 0.1)
	g_i2.beginline(2,1)

	g_v.erase()
	g_i1.erase()
	g_v.size(0, tstop, hold_pot-10, end_cl+10)
	g_i1.size(0, tstop, y_min, 0.1)	

    for (i=st_cl; i<=end_cl; i=i+incr1) {

		erase()
		
		Clamp(i)
		
		tr_vec.line(g_i2,v_vec,2,1)

	}
    
    aa = nctr_vec.max()
    for i=0, v_vec.size()-1 {
    	nctr_vec.x[i]=nctr_vec.x[i]/aa
    }

    g_c.erase()
    g_c.size(st_cl,end_gr,0,1)
    g_c.begin()

    for i=0, v_vec.size()-1 { 
    	if (v_vec.x[i]==end_gr) {
    		v_vec.resize(i)
    		nctr_vec.resize(i)
    		break
    	}
    }

    nctr_vec.line(g_c,v_vec,1,1)
    g_c.flush()
    doNotify()

}


proc Tstop() {
	tstop = volt_cl.dur[0]+volt_cl.dur[1]+volt_cl.dur[2]
}

access soma
execute(cmd)
	
{
xpanel("Run Control", 0)
v_init = -80
xvalue("Init","v_init", 1,"stdinit()", 1, 1 )
xbutton("Init & Run","run()")
xbutton("Stop","stoprun=1")
runStopAt = 5
xvalue("Continue til","runStopAt", 1,"{continuerun(runStopAt) stoprun=1}", 1, 1 )
runStopIn = 1
xvalue("Continue for","runStopIn", 1,"{continuerun(t + runStopIn) stoprun=1}", 1, 1 )
xbutton("Single Step","steprun()")
t = 40
xvalue("t","t", 2 )
tstop = 415
xvalue("Tstop","tstop", 1,"tstop_changed()", 0, 1 )
dt = 0.125
xvalue("dt","dt", 1,"setdt()", 0, 1 )
steps_per_ms = 40
xvalue("Points plotted/ms","steps_per_ms", 1,"setdt()", 0, 1 )
screen_update_invl = 0.05
xvalue("Scrn update invl","screen_update_invl", 1,"", 0, 1 )
realtime = 0.00999999
xvalue("Real Time","realtime", 0,"", 0, 1 )
xpanel(1,102)
}

{
xpanel("Clamp Control", 0)
xbutton("Start","start()")
y_min = -0.2
xvalue("maximal current", "y_min")
st_cl=-80
xvalue("Start voltage", "st_cl")
end_cl=60
xvalue("End voltage", "end_cl")
end_gr=60
xvalue("end graph", "end_gr")
incr1=1
volt_cl.dur[0]=5
xvalue("Pre-pulse duration", "volt_cl.dur[0]", 1, "Tstop()")
volt_cl.dur[1]=400
xvalue("Test pulse duration", "volt_cl.dur[1]", 1, "Tstop()")
volt_cl.dur[2]=10
xvalue("Post-pulse duration", "volt_cl.dur[2]", 1, "Tstop()")
hold_pot=v_init
xvalue("Holding voltage", "hold_pot")
xpanel(1,537)
}


vbox1=new VBox()
vbox1.intercept(1)
g_v=new Graph()
g_v.size(0, tstop, hold_pot-10, end_cl+10)
vbox1.intercept(0)
vbox1.map("Voltage protocol", 319, 24, 625, 260)


vbox2=new VBox()
vbox2.intercept(1)
g_i1=new Graph()
g_i1.size(0, tstop, y_min, 0.1)
vbox2.intercept(0)
vbox2.map("IV currents", 320, 456, 625, 304)


vbox3=new VBox()
vbox3.intercept(1)
g_i2=new Graph()
g_i2.size(st_cl, end_cl, y_min, 0.1)
vbox3.intercept(0)
vbox3.map("IV-relationship", 1161, 454, 306, 298)