begintemplate Ramp
external WindowGroup, WindowGroupItem, nrncontrolmenu, ramp_myo
external set_ion_conc, set_env_cond, stdinit, run, tstop
public myocyte, SEC, V_plot, INa_plot, ICa_plot, INa_states_plot
public gui_create, plot_holder, nav_states
public xsource, ysource, xdest, ramp, base_values, dur_values
objref this, nil, plot_holder
objref myocyte, SEC, xsource, ysource, xdest, ramp, base_values, dur_values
objref V_plot, INa_plot, ICa_plot, INa_states_plot, pwm, gui
strdef tmp_string, filename, label

proc init() {
    create_myocyte(myocyte, $o1)
    pwm = $o2
    // Add SEClamp for Double Step protocol
    /*
    PROTOCOL description:
    amp1 for dur1
    amp2a  for dur2a
    amp2b  for dur2b
    amp2c  for dur2c
    amp3 for dur3
    */
    
    amp2a = -30 // mV
    amp2b = -130 // mV
    amp2c = -30 // mV
    dur2a = 15 // ms
    dur2b = 100 // ms
    dur2c = 15 // ms
    access myocyte.cell
    SEC = new SEClamp(0.5)
    SEC.amp1 = -130 // mV
    SEC.amp2 = -30 // mV
    SEC.amp3 = -30 // mV
    SEC.dur1 = 100 // ms
    SEC.dur2 = 40 // ms
    SEC.dur3 = 10 // ms
    SEC.rs = 0.0001 // MOhm
    
    xdest = new Vector()
    xsource = new Vector(3)
    ysource = new Vector(3)
    
    base_values = new Vector()
    dur_values = new Vector()
}

proc gui_create() {    
    nrncontrolmenu()
    xpanel("Ramp Protocol",0)
    xbutton("Run", "ramp_run()")
    xbutton("Break", "break_loop()")
    xvalue("Base (mV)","this.SEC.amp1",1,"",0,1)
    xvalue("Step to (mV)","this.SEC.amp3",1,"",0,1)
    
    xbutton("Plot V", "plots(\"V\")")
    xbutton("Plot INa", "plots(\"INa\")")
    xbutton("Plot ICa", "plots(\"ICa\")")
    xbutton("Plot INa States", "plots(\"Nav_states\")")
    xbutton("Plot All", "plots(\"all\")")
    xbutton("Close All plots", "close_all()")
    xbutton("Delete protocol and model","delete_this()")
    xpanel()
    gui = new WindowGroupItem(pwm.count()-1, pwm.name(pwm.count()-1))
    
    xsource.x[0] = 0
    xsource.x[1] = SEC.dur1
    
}

proc plots() {
    if (plot_holder == nil) {plot_holder = new Plots(myocyte, pwm)}
    if (strcmp($s1,"V")==0) {
	plot_holder.plot_V()
    }
    if (strcmp($s1,"INa")==0) {
	plot_holder.plot_INa()
    }
    if (strcmp($s1,"ICa")==0) {
	plot_holder.plot_ICa()
    }
    if (strcmp($s1,"Nav_states")==0) {
	plot_holder.plot_INa_states()
    }
    if (strcmp($s1,"all")==0) {
	plot_holder.plot_V()
	plot_holder.plot_INa()
	plot_holder.plot_gNa()
	plot_holder.plot_ICa()
	plot_holder.plot_INa_states()
    }
}
proc create_myocyte() {
    $o1 = new Myocyte("ramp_myo")
    
    access $o1.cell
    $o2.set_myocyte($o1)
    set_ion_conc()
    // params.save_myocyte()
    set_env_cond()
}

proc close_all() {local i
    if (plot_holder!=nil) {
	plot_holder.close_all()
	objref plot_holder
    }
}


proc delete_this() {local i
    if (SEC!=nil) {objref SEC}
    if (myocyte!=nil) {objref myocyte}
    if (plot_holder!=nil) {
	plot_holder.close_all()
	objref plot_holder
    }
    pwm.close(gui.index)    
}

proc ramp_run() {
    breakloop = 0
    if (plot_holder != nil) {
	plot_holder.erase_all()
	sprint(label,"V: mV")
	if (plot_holder.V_plot != nil) {plot_holder.V_plot.addvar(label,&myocyte.cell.v(0.5),1,1)}
	sprint(label,"iNa: mA/cm2")
	if (plot_holder.INa_plot != nil) {plot_holder.INa_plot.addvar(label,&myocyte.cell.ina(0.5),1,1)}
	sprint(label,"gNa: S/cm2")
	if (plot_holder.gNa_plot != nil) {plot_holder.gNa_plot.addexpr(label,"ramp_myo.myocyte.cell.g_NAV_withF(0.5) + ramp_myo.myocyte.cell.g_NAV_noF(0.5)",1,1)}
	sprint(label,"iCa: mA/cm2")
	if (plot_holder.ICa_plot != nil) {plot_holder.ICa_plot.addvar(label,&myocyte.cell.ica(0.5),1,1)}
	plot_holder.populate_nav_states(1)
    }
    for (dur2 = 0; dur2 <= 20; dur2 = dur2+2) {
	if (breakloop) {break}
    	dt = 0.025
	SEC.dur2 = dur2 // ms
	tstop = SEC.dur1 + SEC.dur2 + SEC.dur3
	xdest.indgen(0,tstop,dt)
	xsource.x[2] = SEC.dur1 + SEC.dur2
	// xsource.printf ()
	ysource.x[0] = SEC.amp1
	ysource.x[1] = SEC.amp1
	SEC.amp2 = SEC.amp3
	ysource.x[2] = SEC.amp3
	ramp = ysource.interpolate(xdest, xsource)
	ramp.play(&SEC.amp2, dt)
	sprint(tmp_string, "v_init = %g",SEC.amp1)
	execute(tmp_string)
	stdinit()
	run()
    	if (plot_holder != nil) {plot_holder.view_eq_plot()}
	ramp.play_remove ()
    }
}

proc break_loop() {
    stoprun=1
    breakloop = 1
}

endtemplate Ramp