/* Driver Program for Midbrain Dopaminergic Neuron model */
/*Different from dist.hoc by Ih current hd, #of  synapses, AMPA,NMDA formulation for all dend 0-37, inserting mechanisms in "forsec distDend"
 Prox or Distdend sections not in each dendrite, number of segments (inside each compartemnt) formulation nseg, read i.c. from different files,
print less dend data, writ i.c. in diferent file 
*/
/* 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. */
load_file("dopaminergic.hoc")
timecon = 0
back= 0 /*1*/                         /* 1 switch for running in the background */
sakmann=0
load_file("fixnseg.hoc")
if(!back)load_file("nrngui.hoc")
verbose= 0 /*0*/
v_init = -62.6
vcseries = 0
clamp = 0                     /* switch for voltage clamp*/
restart = 0 /*1*/                      /* switch for initializing from state.old */
tstart = 0
if(vcseries) tstop = 800 else tstop = 10000    /*time in msec*/
if(clamp && !vcseries) tstop= 600
if(timecon) tstop = 800


nsyn = 45			/* The number of synapses */
Dtmax = 1.0  
Dt = 1.00
if(timecon) Dtmax = 1.0 
dt = 5e-1 /*5e-4*/  
if(timecon) dt = 0.01
nainit=  4.075
vsolder=v_init
vsold=v_init

/*PARAMETERS*/
na_cond =  550.0e-6 
kdr_cond = 665.0e-6
ca_cond = 11.196e-6
kca_cond = 59.0e-6
a_cond_s = 570.0e-6
a_cond_p = 285.0e-6
a_cond_d = 266.0e-6
/*stronger gA *1.28 =  729.6, 364.8, 340.48*/
iapl = 0 /*in nA, -0.180nA=-180pA*/

global_ra = 40
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 stim,synn[nsyn],syna[nsyn]
objectvar vc
objref cvode
cvode = new CVode(1) /* 0 for clamp*/
x= cvode.active(1)

proc init_cell() {
/* First set all of the dimensions and insert the channels into each
 * section. */



forsec  "soma"{

	{insert nabalan}
        {insert hh3 gnabar_hh3 = na_cond gkhhbar_hh3 = kdr_cond  gkabar_hh3 = a_cond_s
          qs_hh3 = 56.0 qv_hh3 = 8.0}
        {insert pump}
        {insert leak}
        {insert cabalan}
        {insert cachan gcalbar_cachan = ca_cond gkbar_cachan = 0.0}
        {insert kca gkbar_kca = kca_cond}
        // {insert capump}


    }

soma {
        stim = new MyIClamp(.5)
        {stim.del = 4000 stim.dur = 1000.0 stim.amp = 0.00  /*in nA, -0.180nA=-180pA*/
         stim.amp2 = iapl}
}

forsec  proxDend {
	
	{insert nabalan}
	{insert hh3 gnabar_hh3 = na_cond  gkhhbar_hh3 = kdr_cond gkabar_hh3 = a_cond_p
          qv_hh3 = 60.0 qs_hh3 = 5.0}
	{insert pump}
        {insert leak}
        {insert cabalan}
        {insert cachan gcalbar_cachan = ca_cond gkbar_cachan = 0.0 }
        {insert kca gkbar_kca = kca_cond}
        
	}

forsec  distDend {
	
	{insert nabalan}
	{insert hh3 gnabar_hh3 = na_cond  gkhhbar_hh3 = kdr_cond gkabar_hh3 = a_cond_d
          qv_hh3 = 60.0 qs_hh3 = 5.0}
	{insert pump}
        {insert leak}       
        {insert cabalan}
        {insert cachan gcalbar_cachan = ca_cond gkbar_cachan = 0.0 }
        {insert kca gkbar_kca = kca_cond}
        
	}
    
/*     for i = 0,37 dend[i] { 
       synn[i] = new NMDA(.5)
       synn[i].synstim = 2000.0 synn[i].Pbar  = 0.10e-6 synn[i].isi = 20.0 synn[i].N = 50 synn[i].gmax = 0.005
       syna[i] = new AMPA(0.5)
       syna[i].synstim = 2000.0 syna[i].gmax  = 0.0 syna[i].isi = 20.0 syna[i].N = 50
}
 dend[26] {
       synn[38] = new NMDA(0.8)
       synn[38].synstim = 2000.0 synn[38].Pbar  = 0.10e-6 synn[38].isi = 20.0 synn[38].N = 50 synn[38].gmax = 0.005
       syna[38] = new AMPA(0.8)
       syna[38].synstim = 2000.0 syna[38].gmax  = 0.0 syna[38].isi = 20.0 syna[38].N = 50
       synn[39] = new NMDA(0.2)
       synn[39].synstim = 2000.0 synn[39].Pbar  = 0.10e-6 synn[39].isi = 20.0 synn[39].N = 50 synn[39].gmax = 0.005
       syna[39] =new AMPA(0.2)
       syna[39].synstim = 2000.0 syna[39].gmax  = 0.0 syna[39].isi = 20.0 syna[39].N = 50
}
 dend[36] {
       synn[40] = new NMDA(0.8)
       synn[40].synstim = 2000.0 synn[40].Pbar  = 0.10e-6 synn[40].isi = 20.0 synn[40].N = 50 synn[40].gmax = 0.005
       syna[40] =new AMPA(0.8)
       syna[40].synstim = 2000.0 syna[40].gmax  = 0.0 syna[40].isi = 20.0 syna[40].N = 50
       synn[41] = new NMDA(0.2)
       synn[41].synstim = 2000.0 synn[41].Pbar  = 0.10e-6 synn[41].isi = 20.0 synn[41].N = 50 synn[41].gmax = 0.005
       syna[41] =new AMPA(0.2)
       syna[41].synstim = 2000.0 syna[41].gmax  = 0.0 syna[41].isi = 20.0 syna[41].N = 50
}
 dend[35] {
       synn[42] = new NMDA(0.8)
       synn[42].synstim = 2000.0 synn[42].Pbar  = 0.10e-6 synn[42].isi = 20.0 synn[42].N = 50 synn[42].gmax = 0.005
       syna[42] =new AMPA(0.8)
       syna[42].synstim = 2000.0 syna[42].gmax  = 0.0 syna[42].isi = 20.0 syna[42].N = 50
       synn[43] = new NMDA(0.2)
       synn[43].synstim = 2000.0 synn[43].Pbar  = 0.10e-6 synn[43].isi = 20.0 synn[43].N = 50 synn[43].gmax = 0.005
       syna[43] =new AMPA(0.2)
       syna[43].synstim = 2000.0 syna[43].gmax  = 0.0 syna[43].isi = 20.0 syna[43].N = 50
}

 dend[30] {
       synn[44] = new NMDA(0.8)
       synn[44].synstim = 2000.0 synn[44].Pbar  = 0.10e-6 synn[44].isi = 20.0 synn[44].N = 50 synn[44].gmax = 0.005
       syna[44] =new AMPA(0.8)
       syna[44].synstim = 2000.0 syna[44].gmax  = 0.0 syna[44].isi = 20.0 syna[44].N = 50
      
}
*/

forall cm = global_cm
forall Ra = global_ra
g_celsius = 35

    }

tot=0
forall {tot=tot+nseg}
print "segments before ", tot
nseg=1

// Increase number of segments
geom_nseg()

tot=0
forall {tot=tot+nseg}
print "segments after ", tot

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

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

proc init() {local i

access soma

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

} else {
           fcurrent()
        }
 frecord_init()
           }
        t = tstart
}
init()
if(back){
if(!clamp ||verbose) {print t/1000,soma[1].v(0.5),soma[1].nai(0.5),soma[1].cai(0.5),dend[3].v(0.5),dend[3].cai(0.5),dend[17].v(0.5),dend[17].cai(0.5)}
if(clamp && !vcseries && !verbose) print t/1000,vc.i,soma.cai(0.5),soma.v(0.5)
if(vcseries) j= 10 else j = 0
for i = 0, j { if (vcseries) vc.amp1 = vc.amp1 + 10}
if(vcseries) init()
next = t + Dt
flag1=0
flag2=0
hold = 0
while (t<tstop){
          vsolder=vsold
          vsold=soma.v(0.5)

          fadvance()
           if(!clamp||verbose){
          if((vsolder<vsold &&soma.v(0.5) <vsold)||(vsolder>vsold && soma.v(0.5)>vsold)) {
          vsolder=soma.v(0.5)
print t/1000,soma[1].v(0.5),soma[1].nai(0.5),soma[1].cai(0.5),dend[3].v(0.5),dend[3].cai(0.5),dend[17].v(0.5),dend[17].cai(0.5)
               next = t + Dt
                           flag2=1
                           hold=dt
          soma.v(0.5)=vsolder
                            }
                            }   
               if(t>=next){
               Dt = 100*dt
               if(Dt>Dtmax) Dt = Dtmax
               if (Dt<.1) Dt = .1
               next = t + Dt
         if(!clamp||verbose) {print t/1000,soma[1].v(0.5),soma[1].nai(0.5),soma[1].cai(0.5),dend[3].v(0.5),dend[3].cai(0.5),dend[17].v(0.5),dend[17].cai(0.5)}
if(clamp && !vcseries && !verbose) print t/1000,vc.i,soma.cai(0.5),soma.v(0.5)
                          }
                 }
print t/1000,soma[1].v(0.5),soma[1].nai(0.5),soma[1].cai(0.5),dend[3].v(0.5),dend[3].cai(0.5),dend[17].v(0.5),dend[17].cai(0.5)
if(vcseries) print vc.amp1,vc.i
f2.wopen("state.new")
ss.save()
ss.fwrite(f2)
f2.close
} else { 
                   forall Ra = global_ra
                   forall cm = global_cm
                   celsius = g_celsius
       }
//load_file("damodel.ses")
//