begintemplate Simul

objref stim

public soma, dend, run, add_stim
create soma, dend[16]

proc init() {local i

    create soma, dend[16]
    access soma

    L = 40
    diam = 40
    nseg = 1
    Ra = 69.9986
    insert pas
    g_pas = 0.000091
    e_pas = -60.0

    dend[0] {  L = 265.791375  diam = 2.00000000   nseg = 14 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[1] {  L = 265.791375  diam = 2.00000000   nseg = 14 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[2] {  L = 265.791375  diam = 2.00000000   nseg = 14 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[3] {  L = 325.526623  diam = 3.00000000   nseg = 17 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[4] {  L = 325.526623  diam = 3.00000000   nseg = 17 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[5] {  L = 205.068923  diam = 4.76220316   nseg = 11 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[6] {  L = 191.668747  diam = 4.16016760   nseg = 11 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[7] {  L =  83.404623  diam = 7.08975084   nseg = 6  Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[8] {  L = 250.590512  diam = 4.00000000   nseg = 14 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[9] {  L = 250.590512  diam = 4.00000000   nseg = 14 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[10] {  L = 198.109194  diam = 2.50000000  nseg = 11 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[11] {  L = 198.109194  diam = 2.50000000  nseg = 11 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[12] {  L = 198.109194  diam = 2.50000000  nseg = 11 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[13] {  L = 285.722900  diam = 5.20020956  nseg = 15 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[14] {  L = 315.724260  diam = 6.34960420  nseg = 17 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[15] {  L = 189.914193  diam = 9.18978988  nseg = 11 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }

    connect dend[15](0), soma(1)
    for i=13,14 connect dend[i](0), dend[15](1)
    for i=10,12 connect dend[i](0), dend[13](1)
    for i=8,9 connect  dend[i](0),dend[14](1)

    connect dend[7](0), soma(1)
    for i=5,6 connect dend[i](0), dend[7](1)
    for i=3,4 connect dend[i](0), dend[5](1)
    for i=0,2 connect dend[i](0), dend[6](1)
}

proc add_stim() {
    dend[$1] stim = new IClamp($2)
    stim.del = 0
    stim.dur = 10
    stim.amp = 1
}

proc run() {

    dt = 0.001
    v_init = -60
    finitialize(-60)

    for i=1,10 {
        while (t <= i) {
            secondorder = 2
            fadvance()
        }
        fprint("%f \t",-(60 + v(0.5)) )
    }
    fprint("\n")
}

endtemplate Simul

objref f1
f1 = new File()

f1.ropen("CellData.dat")
wopen("NResults.res")

objectvar Simuls
    Simuls = new Simul( )

// for i=0,1999 {
for i=0,9 { // there is enough data here for 10 runs
    for j=0,199 {
        Simuls.add_stim( f1.scanvar(), f1.scanvar() )
    }
    Simuls.run()
    print "Simulation", i "complete"
}

f1.ropen()
wopen()