strdef cmd, isomer
objref volt_cl			  // variable for Point-Process
objref vbox1, vbox2		  // graphs variables
objref g_v, g_i	          // graphs variables

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 start() {
	volt_cl.amp[0] = hold_pot
	volt_cl.amp[2] = hold_pot
	
	g_v.erase()
	g_i.erase()
	g_v.size(0, tstop, hold_pot-10, end_cl+10)
	g_i.size(0, tstop, y_min, 0.1)

	for (i=st_cl; i<=end_cl; i=i+incr1) {
       volt_cl.amp[1]=i
       
       finitialize(v_init)
       g_v.beginline()
       g_i.beginline()
       
       while (t<tstop) {
       		
			// clamping current without capacitive current, in mA/cm2       		
       		dens=volt_cl.i/area(.5)*100-soma.i_cap(0.5) 
       		
       		g_i.line(t, dens)
       		g_v.line(t, soma.v(.5))
       		
       		fadvance()
       }
    }
}

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
	}
	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
	}
	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
	}
	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
	}
	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
	}
	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
	}
	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
	}
	if ($1 == 8) {
		isomer = "na18a"
		y_min=-1.1
		volt_cl.dur[0]=5
		volt_cl.dur[1]=100
		volt_cl.dur[2]=5
		hold_pot=-70
		tstop=110
	}
	if ($1 == 9) {
		isomer = "na19a"
		y_min=-3.3
		volt_cl.dur[0]=1
		volt_cl.dur[1]=50
		volt_cl.dur[2]=5
		hold_pot=-120
		tstop=56
	}
}

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.025
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()")
st_cl=-80
xvalue("start clamp", "st_cl")
end_cl=70
xvalue("end clamp", "end_cl")
incr1=5
xvalue("incr clamp", "incr1")
volt_cl.dur[0]=1
xvalue("1st step dur", "volt_cl.dur[0]")
volt_cl.dur[1]=15
xvalue("2nd step dur", "volt_cl.dur[1]")
volt_cl.dur[2]=2
xvalue("3rd step dur", "volt_cl.dur[2]")
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_i=new Graph()
g_i.size(0, tstop, y_min, 0.1)
vbox2.intercept(0)
vbox2.map("Soma clamp current", 520, 20, 600, 450)