// Author: Ronald van Elburg  (RonaldAJ at vanElburg eu)
//
// NEURON script for the paper:
//
//   Ronald A.J. van Elburg and Arjen van Ooyen (2010) `Impact of dendritic size and
//   dendritic topology on burst firing in pyramidal cells', 
//   PLoS Comput Biol 6(5): e1000781. doi:10.1371/journal.pcbi.1000781.
//
// Please consult readme.txt or instructions on the usage of this file.
//
// This software is released under the GNU GPL version 3: 
// http://www.gnu.org/copyleft/gpl.html

objectvar save_window_, rvp_
objectvar scene_vector_[4]
objectvar ocbox_, ocbox_list_, scene_, scene_list_

load_file("../modelSimplifiedCells.hoc")
tstop = 5000  //tstop = 10000 used in paper 

//Transform MRC_Slice(No) number to multidimensional quantity
double MRC_SliceIndex[3]
NaSliceNo=int(MRC_Slice/4)                  // Combined 2D Km-KCa ranges contains four points
KmSliceNo=int((MRC_Slice-4*NaSliceNo)/2)    // KCa ranges contains two points
KCaSliceNo=MRC_Slice-4*NaSliceNo-2*KmSliceNo

MRC_SliceIndex[0]=NaSliceNo
MRC_SliceIndex[1]=KmSliceNo
MRC_SliceIndex[2]=KCaSliceNo



proc customModifications(){
    setNa("dend")
    setKm("dend")
    setKCa("dend")
}

proc setNa(){local sectionLength localobj allSections 
    allSections=new SectionList()
    
    forsec $s1 {
        allSections.append()
    }
    
    forsec allSections {
        if(bActiveDendrites==1){
            gbar_na =   15*NaFactor        // fast Na+  
        }      
    }
}  


proc setKm(){local sectionLength localobj allSections 
    allSections=new SectionList()
    
    forsec $s1 {
        allSections.append()
    }
    
    
    forsec allSections {
        if(bActiveDendrites==1){
            gbar_km =  0.1*KmFactor          // slow voltage dependent non-inactivating K+ 
        }      
    }
}  


proc setKCa(){local sectionLength localobj allSections 
    allSections=new SectionList()
    
    forsec $s1 {
        allSections.append()
    }
    
    
    forsec allSections {
        if(bActiveDendrites==1){
            gbar_kca =   3*KCaFactor         // slow Ca++ activated K+
        }      
    }
}  

{ocbox_list_ = new List()  scene_list_ = new List()}
{
    xpanel("RunControl", 0)
        
        v_init = -70
        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 = 0
        xvalue("t","t", 2 )
        
        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 = 1
        xvalue("Scrn update invl","screen_update_invl", 1,"", 0, 1 )
        
        realtime = 0
        xvalue("Real Time","realtime", 0,"", 0, 1 )
    xpanel(-1,1500)
}
{
save_window_ = new Graph(0)
save_window_.size(0,1500,-80,40)
scene_vector_[3] = save_window_
{save_window_.view(0, -80, 1500, 120, 295, 661, 300.6, 200.8)}
graphList[0].append(save_window_)
save_window_.save_name("graphList[0].")
save_window_.addexpr("v(.5)", 1, 1, 0.8, 0.9, 2)
}
{
save_window_ = new PlotShape(0)
save_window_.size(-346.486,1827.84,-58.5592,2115.77)
save_window_.variable("v")
scene_vector_[2] = save_window_
{save_window_.view(-346.486, -58.5592, 2174.33, 2174.33, 714, 661, 200.7, 200.8)}
fast_flush_list.append(save_window_)
save_window_.save_name("fast_flush_list.")
}

//Begin MultipleRunControlGUI[0]
{
load_file("../hoc/mrc/MultipleRunControl.hoc","MultipleRunControlGUI")
}



{
ocbox_ = new MultipleRunControlGUI(1)
}
{object_push(ocbox_)}
{
file_name="SimplifiedTopologiesGKCaGActScan/Results/Sim"
file_index_start=0
}

{tobj=new MRC_Protocol()}
	{object_push(tobj)}
	{
		output_matlab_mfile=0
		output_neuronbinary=1
		output_axontextfile=0
	}
	{object_pop()}

{protocol=tobj}




{tobj=new MRC_LoopParameter()}
	{object_push(tobj)}
	{
        name="NaFactor"
		lower_limit=0.9
		upper_limit=1.1
		stepsize=0.2
        restart=2
        restart_at=lower_limit+ MRC_SliceIndex[0]*stepsize
        end_before=lower_limit+( MRC_SliceIndex[0]+1)*stepsize
		use=1
		setdisplaytext()
	}
	{object_pop()}
{looppars.append(tobj)}

{tobj=new MRC_LoopParameter()}
	{object_push(tobj)}
	{   
		name="KmFactor"
		lower_limit=0.7
		upper_limit=1.3
		stepsize=0.6                    //stepsize=0.2 used in paper
        restart=2
        restart_at=lower_limit+MRC_SliceIndex[1]*stepsize
        end_before=lower_limit+(MRC_SliceIndex[1]+1)*stepsize
		use=1
		setdisplaytext()
	}
	{object_pop()}
{looppars.append(tobj)}

{tobj=new MRC_LoopParameter()}
	{object_push(tobj)}
	{
       
		name="KCaFactor"
		lower_limit=0.7
		upper_limit=1.3
		stepsize=0.6                    //stepsize=0.2 used in paper
        restart=2
        restart_at=lower_limit+MRC_SliceIndex[2]*stepsize
        end_before=lower_limit+(MRC_SliceIndex[2]+1)*stepsize
		use=1
		setdisplaytext()
	}
	{object_pop()}
{looppars.append(tobj)}

{tobj=new MRC_LoopParameter()}
	{object_push(tobj)}
	{
		name="currentTopologyNo"
		lower_limit=1
		upper_limit=23
		stepsize=11       // stepsize=1  used in paper
		use=1
		setdisplaytext()
	}
	{object_pop()}
{looppars.append(tobj)}

{tobj=new MRC_LoopParameter()}
	{object_push(tobj)}
	{
		name="bStimSoma"
		lower_limit=0 // lower_limit=0 used in paper
		upper_limit=1
		stepsize=1
		use=1
		setdisplaytext()
	}
	{object_pop()}
{looppars.append(tobj)}

{tobj=new MRC_LoopParameter()}
	{object_push(tobj)}
	{
		name="totalDendriticLength"
		lower_limit=1000
		upper_limit=3500
		stepsize=500    // stepsize=25 used in paper
		use=1
		setdisplaytext()
	}
	{object_pop()}
{looppars.append(tobj)}


{tobj1=types_outpar.gettypefromindex(2)}
{tobj=new MRC_OutputVariable("spikes",tobj1,protocol)}
	{object_push(tobj)}
	{
		use=1
		setdisplaytext()
	}
	{object_pop()}
{tobj1=tobj.gethandler()}
	{object_push(tobj1)}
	{
		record_start=0
		record_stop=9000
		threshold=0
		listname="soma"
		sectionname=""
		membername="v(0.5)"
		listtype=2
		useindexing=0
		isart=0
		shortname="spikes"
	}
	{object_pop()}
{outpars.append(tobj)}

{tobj1=types_outpar.gettypefromindex(1)}
{tobj=new MRC_OutputVariable("vtrace_soma",tobj1,protocol)}
	{object_push(tobj)}
	{
		use=0
		setdisplaytext()
	}
	{object_pop()}
{tobj1=tobj.gethandler()}
	{object_push(tobj1)}
	{
		record_start=0
		record_stop=9000
		sectionname="soma"
		membername="v(0.5)"
		useindexing=0
		isart=0
		shortname="vtrace_soma"
	}
	{object_pop()}

{outpars.append(tobj)}




{object_pop()}
{
ocbox_.map("MultipleRunControlGUI[0]", 931, 134, 399.6, 385.2)
}
objref ocbox_
//End MultipleRunControlGUI[0]

objectvar scene_vector_[1]
{doNotify()}

if(name_declared("MRC_Slice")==5){
 execute("looprun()",MultipleRunControlGUI[0])
}