/* Driver Program for Midbrain Dopaminergic Neuron model */

/* The following command loads the standard run library functions into
 * the simulator.  These library functions are used to initialize the
 * simulator, begin and end simulations, and plot variables during a
 * simulation. */
timecon = 0
back= 1                         /* switch for running in the background */
sakmann=0
if(!back)load_proc("nrnmainmenu")
verbose=0
v_init = -60
vcseries = 0
clamp = 0                      /* switch for voltage clamp*/
restart = 0                      /* switch for initializing from state.old */
tstart = 0
if(vcseries) tstop = 800 else tstop = 100000
/*if(timecon) tstop = 100*/
if(timecon) tstop = 1000
nsyn = 1			/* The number of synapses */
soma_len= 15
soma_diam= 15
ndend=4
dend_diam= 1.0  /* need to make dendrite taper */
prox_diam= 2.0  /* need to make dendrite taper */
taper = 1.0
dend_len = 500
prox_len = 150
Dt =   5.0
if(timecon) Dt = 1.0 
dt =    1.0
if(timecon) dt = 0.01
nainit=  4.000


create soma, prox[ndend], dend[ndend]	/* Create all of the cell's sections */
global_ra = 200
forall Ra = global_ra
global_cm = 1.0
forall cm = global_cm
g_celsius = 35
celsius = g_celsius
forall ion_style("na_ion", 2,2,0,0,0)
access soma			/* All default references are to soma */

objectvar syn[nsyn]		/* Declare the object variables for
				 * the three synapses */
objectvar stim
objectvar vc
objref cvode
cvode = new CVode(1)
// if stdrun code has been loaded then update the variable step control,
// otherwise the following will fall through and background control will
// still work
if (name_declared("cvode_active")) {
	cvode_active(1)
}

proc init_cell() {
/* First set all of the dimensions and insert the channels into each
 * section. */
    soma {
	nseg = 1		/* # of compartments */
	diam = soma_diam		/* Set the diameter of the soma (in um) */
	L = soma_len			/* Set the length of the soma (in um) */

        {insert nadifl D_nadifl = 0.60  }
        {insert leak gkbar_leak =   100   gnabar_leak=   40 ek_leak = -100 ggabaa_leak = 0 }
        {insert delay gkbar_delay = 400 } 
        { insert pump ipumpmax_pump = 0.012  }
if (!clamp) {
        istim = 0.022  /* 0.022*/
	stim = new IClamp(.5)
        {stim.del=0 stim.dur=200000 stim.amp=-istim*3.1416*diam*L*(0.01)} 
if(timecon) stim.dur=0.5
if (sakmann) stim.amp = -0.063
if(timecon) stim.amp= -1.0
     } else {
	vc = new VClamp1(.5)
        vc.vset = -130.0
        vc.gain = 0.5
/*vc.amp[0] = -65 
	vc.dur[0] = tstop + 1
	vc.amp[1] = -50
	vc.dur[1] = 1000
	vc.amp[2] = -60
	vc.dur[2] = 1000
        vc.gain = 0.5e5
        vc.tau1 = 0
        vc.tau2 = 0 */
    }

	pt3dclear()
	pt3dadd(0,0,0,soma_diam)
	pt3dadd(soma_len,0,0,soma_diam)

    }
 
    for i = 0, ndend-1 prox[i] {
	nseg = 3	/* # of compartments */
	L = prox_len			/* Set the length of the dendrite (in um) */
        {insert leak gkbar_leak =  100 gnabar_leak=  40 ek_leak = -100 }
        {insert delay gkbar_delay=400 }
	{insert  nmda Pbar_nmda = 0.00e-6
                 base_nmda = 0.000 km_nmda= 50.75 q_nmda = 9  }
        {insert nadifl D_nadifl = 0.60  }
        { insert pump ipumpmax_pump = 0.012  }
forall cm = global_cm
forall Ra = global_ra
g_celsius = 35
    }
 
    for i = 0, ndend-1 dend[i] {
	nseg = 7	/* # of compartments */
	L = dend_len - prox_len			/* Set the length of the dendrite (in um) */
        {insert leak gkbar_leak =  100 gnabar_leak=  40 ek_leak = -100 }
        {insert delay gkbar_delay=400 } 
	{insert  nmda Pbar_nmda = 1.4e-6
                 pr_nmda = 0.05 base_nmda = 0.000 km_nmda= 50.75 q_nmda = 9.0 }
        {insert nadifl D_nadifl = 0.60  }
        { insert pump ipumpmax_pump = 0.012  }
forall cm = 5*global_cm
forall Ra = global_ra
g_celsius = 35
    }

/* Next we construct the topology by connecting each of the sections
 * together. */
    connect prox[0](0), soma(0)
    connect dend[0](0), prox[0](1)
    connect prox[1](0), soma(1)
    connect dend[1](0), prox[1](1)
    connect prox[2](0), soma(0.5)
    connect dend[2](0), prox[2](1)
    connect prox[3](0), soma(0.5) 
    connect dend[3](0), prox[3](1)


        prox[0]{
	pt3dclear() 
	pt3dadd(0,0,0,prox_diam)
	pt3dadd(-prox_len,0,0,prox_diam)
               }
        dend[0]{
	pt3dclear() 
	pt3dadd(-prox_len,0,0,dend_diam)
	pt3dadd(-dend_len,0,0,dend_diam/taper)
               }
        prox[1]{
	pt3dclear() 
	pt3dadd(soma_len,0,0,prox_diam)
	pt3dadd(soma_len+prox_len,0,0,prox_diam)
               }
        dend[1]{
	pt3dclear() 
	pt3dadd(soma_len+prox_len,0,0,dend_diam)
	pt3dadd(soma_len+dend_len,0,0,dend_diam/taper)
               }
        prox[2]{
	pt3dclear() 
	pt3dadd(0,soma_diam/2,0,prox_diam)
	pt3dadd(0,(soma_diam/2 + prox_len),0,prox_diam)
               }
        dend[2]{
	pt3dclear() 
	pt3dadd(0,(soma_diam/2 + prox_len),0,dend_diam)
	pt3dadd(0,soma_diam/2 + dend_len,0,dend_diam/taper)
               }
        prox[3]{
	pt3dclear() 
	pt3dadd(0,-soma_diam/2,0,prox_diam)
	pt3dadd(0,(-soma_diam/2 - prox_len),0,prox_diam)
               }
        dend[3]{
	pt3dclear() 
	pt3dadd(0,-soma_diam/2 - prox_len,0,dend_diam)
	pt3dadd(0,-soma_diam/2 - dend_len,0,dend_diam/taper)
               }
}



init_cell()			/* Call the function to initialize our
				 * cell. */
topology()

objref ss,f1,f2
ss = new SaveState()
f1 = new File()
f2 = new File()



proc init() {local i
	if(!restart) finitialize(v_init)
        fcurrent()
	if (restart){
           f1.ropen("state.old")
           ss.fread(f1)
           f1.close
           finitialize(v_init)
           ss.restore()
           fcurrent()
           cvode.re_init()
        }
        t = tstart
}




/*
proc continuerun() {local rt
	eventcount=0
	eventslow=1
	stoprun = 0
        startsw()
	while(t < $1 && stoprun == 0) {
		step()
		rt = stopsw()
		if (rt > realtime) {
			realtime = rt
			fastflushPlot()
			doNotify()
			if (realtime == 2 && eventcount > 50) {
				eventslow = int(eventcount/50) + 1
			}
			eventcount = 0
		}else{
			eventcount = eventcount + 1
			if ((eventcount%eventslow) == 0) {
				doEvents()
			}
		}
	}
	f2.wopen("state.new")
	ss.save()
	ss.fwrite(f2)
	f2.close
	flushPlot()
}
*/

init()
/*
startsw()
flushPlot()
*/

/*
if(back){
	if(!clamp ||verbose)print t,soma.v(0.5),dend[0].v(0.99),soma.nai(0.5),dend[0].nai(0.99),dend[0].inapump_pump(0.99),dend[0].inmda_nmda(0.99)
	if(clamp && !vcseries && !verbose) print t,vc.i,soma.nai(0.5),soma.v(0.5)
	if(vcseries) j= 10 else j = 0
	for i = 0, j { if (vcseries) vc.vset = vc.vset + 10
		if(vcseries) init()
		while (t<tstop){
			next = t + Dt
			while (t<next) {
                        	fadvance()
		        }
			if(!clamp||verbose) print t,soma.v(0.5),dend[0].v(0.99),soma.nai(0.5),dend[0].nai(0.99),dend[0].inapump_pump(0.99),dend[0].inmda_nmda(0.99)
			if(clamp && !vcseries  && !verbose) print t,vc.i,soma.nai(0.5),soma.v(0.5)
		}
		if(vcseries) print vc.vset,vc.i
		f2.wopen("state.new")
		ss.save()
		ss.fwrite(f2)
		f2.close
        }
} else{
	if(clamp) {xopen("clamp.ses")
        	forall Ra = global_ra
                forall cm = global_cm
                prox{ forall cm = global_cm}
                dend{ forall cm = global_cm}
                celsius = g_celsius
	} else {
		xopen("seg1.ses")
	}
        forall Ra = global_ra
	forall cm = global_cm
	prox{ forall cm = global_cm}
	dend{ forall cm = global_cm}
	celsius = g_celsius
}
*/