strdef cmd, isomer
objref volt_cl			  				// variable for Point-Process
objref vbox1, vbox2, vbox3, vbox4		// graphs variables
objref g_v, g_i1, g_i2, g_c 			// graphs variables
objref v_vec, nctr_vec, tr_vec			// vectors for calculated graphs

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

y_min = -0.9

create soma
soma {
	diam = 50        // micron
	L    = 63.66198  // micron, so that area = 10000 micron2
	nseg = 1 	     // dimensionless
	cm   = 1         // uF/cm2
	Ra   = 70        // ohm-cm
	
	volt_cl = new VClamp_plus(.5)
}

proc Clamp() {

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

     soma_curr_tr=0        // peak current value inizialization

     finitialize(v_init)

     pre_i = 0

     while (t<tstop) {

             dens=volt_cl.i/area(.5)*100-soma.i_cap(.5) // clamping current in mA/cm2
             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)) {

             		sprint(cmd, "%s%s", "soma_cond_tr=g_", isomer)
             		execute(cmd)
                	soma_curr_tr=dens
            	}
             }
 
         	fadvance()
         	pre_i=dens
     }

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

     doEvents()
}

// procedure for graph erasing

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-20, end_cl+20, 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 { // reduces vectors for graphic to v=end_gr
    	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 change_isomer() {
	num_to_strg(num_iso)
	sprint (cmd, "%s %s", "uninsert", isomer)
	execute(cmd)
	
	num_to_strg(nw_num_iso)
	sprint (cmd, "%s %s", "insert", isomer)
	execute(cmd)
	
	num_iso = nw_num_iso
}	
	
proc num_to_strg() {
	if ($1 == 1) {
		isomer = "na11a"
		y_min=-0.9
		volt_cl.dur[0]=1
		volt_cl.dur[1]=15
		volt_cl.dur[2]=2
		hold_pot=-120
		tstop=18
		end_gr=20
		st_cl=-80
		end_cl=60
	}
	if ($1 == 2) {
		isomer = "na12a"
		y_min=-0.9
		volt_cl.dur[0]=1
		volt_cl.dur[1]=10
		volt_cl.dur[2]=2
		hold_pot=-120
		tstop=13
		end_gr=20
		st_cl=-80
		end_cl=60
	}
	if ($1 == 3) {
		isomer = "na13a"
		y_min=-0.9
		volt_cl.dur[0]=1
		volt_cl.dur[1]=20
		volt_cl.dur[2]=2
		hold_pot=-90
		tstop=23
		end_gr=20
		st_cl=-100
		end_cl=60
	}
	if ($1 == 4) {
		isomer = "na14a"
		y_min=-0.8
		volt_cl.dur[0]=1
		volt_cl.dur[1]=12
		volt_cl.dur[2]=2
		hold_pot=-120
		tstop=15
		end_gr=20
		st_cl=-80
		end_cl=60
	}
	if ($1 == 5) {
		isomer = "na15a"
		y_min=-1.1
		volt_cl.dur[0]=1
		volt_cl.dur[1]=20
		volt_cl.dur[2]=2
		hold_pot=-120
		tstop=23
		end_gr=10
		st_cl=-90
		end_cl=60
	}
	if ($1 == 6) {
		isomer = "na16a"
		y_min=-1.7
		volt_cl.dur[0]=1
		volt_cl.dur[1]=7.5
		volt_cl.dur[2]=2
		hold_pot=-90
		tstop=10.5
		end_gr=10
		st_cl=-80
		end_cl=80
	}
	if ($1 == 7) {
		isomer = "na17a"
		y_min=-1.5
		volt_cl.dur[0]=1
		volt_cl.dur[1]=25
		volt_cl.dur[2]=2
		hold_pot=-140
		tstop=28
		end_gr=20
		st_cl=-80
		end_cl=60
	}
	if ($1 == 8) {
		isomer = "na18a"
		y_min=-1.1
		volt_cl.dur[0]=5
		volt_cl.dur[1]=50
		volt_cl.dur[2]=5
		hold_pot=-70
		tstop=60
		end_gr=50
		st_cl=-80
		end_cl=60
	}
	if ($1 == 9) {
		isomer = "na19a"
		y_min=-2.5
		volt_cl.dur[0]=1
		volt_cl.dur[1]=150
		volt_cl.dur[2]=5
		hold_pot=-120
		tstop=106
		end_gr=-10
		st_cl=-100
		end_cl=40
	}
}

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

access soma
num_iso = 1
nw_num_iso = num_iso
num_to_strg(num_iso)
sprint (cmd, "%s %s", "insert", isomer)
execute(cmd)
variable_domain("nw_num_iso", 1, 9)
	
{
xpanel("RunControl", 0)
v_init = -120
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 = 18
xvalue("t","t", 2 )
tstop = 18
xvalue("Tstop","tstop", 1,"tstop_changed()", 0, 1 )
dt = 0.0125
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(3,102)
}

{
xpanel("Avvio", 0)
xbutton("Avvio","start()")
celsius=22
xvalue("celsius")
ena=65
xvalue("ena")
xvalue("isomer (1-9)","nw_num_iso",1,"change_isomer()")
y_min = -0.9
xvalue("maximal current", "y_min")
st_cl=-80
xvalue("start clamp", "st_cl")
end_cl=60
xvalue("end clamp", "end_cl")
end_gr=20
xvalue("end graph", "end_gr")
incr1=1
volt_cl.dur[0]=1
xvalue("1st step dur", "volt_cl.dur[0]", 1, "Tstop()")
volt_cl.dur[1]=15
xvalue("2nd step dur", "volt_cl.dur[1]", 1, "Tstop()")
volt_cl.dur[2]=2
xvalue("3rd step dur", "volt_cl.dur[2]", 1, "Tstop()")
hold_pot=v_init
xvalue("holding_pot", "hold_pot")
xpanel(280,102)
}

// graph for soma voltage

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("Membrane voltage", 3, 500, -1, 0)

// graph for clamping current (clamp.i)

vbox2=new VBox()
vbox2.intercept(1)
g_i1=new Graph()
g_i1.size(0, tstop, y_min, 0.1)
vbox2.intercept(0)
vbox2.map("Soma clamp current", 520, 10, 500, 350)

// graph for peak current/voltage relationship

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("Peak current-voltage relation", 520, 400, 500, 350)

// graph for normalized conductance/voltage relatioship

vbox4=new VBox()
vbox4.intercept(1)
g_c=new Graph()
g_c.size(st_cl,end_gr,0,1)
vbox4.intercept(0)
vbox4.map("Normalized conductance (peak)", 1050, 10, 500, 350)