begintemplate Simul

objref stim[75]

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 = 27 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[1] {  L = 265.791375  diam = 2.00000000   nseg = 27 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[2] {  L = 265.791375  diam = 2.00000000   nseg = 27 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[3] {  L = 325.526623  diam = 3.00000000   nseg = 33 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[4] {  L = 325.526623  diam = 3.00000000   nseg = 33 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[5] {  L = 205.068923  diam = 4.76220316   nseg = 21 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[6] {  L = 191.668747  diam = 4.16016760   nseg = 20 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[7] {  L =  83.404623  diam = 7.08975084   nseg =  9 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[8] {  L = 250.590512  diam = 4.00000000   nseg = 26 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[9] {  L = 250.590512  diam = 4.00000000   nseg = 26 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[10] {  L = 198.109194  diam = 2.50000000  nseg = 20 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[11] {  L = 198.109194  diam = 2.50000000  nseg = 20 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[12] {  L = 198.109194  diam = 2.50000000  nseg = 20 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[13] {  L = 285.722900  diam = 5.20020956  nseg = 29 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[14] {  L = 315.724260  diam = 6.34960420  nseg = 32 Ra = 69.9986 insert pas g_pas = 0.000091 e_pas = -60.0 }
    dend[15] {  L = 189.914193  diam = 9.18978988  nseg = 20 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[$2] = new IClamp($3)
    stim[$2].del = 0
    stim[$2].dur = 10
    stim[$2].amp = 0.02
}

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("InputDendrite.dat")

objref f2
f2 = new File()

f2.ropen("InputCurrents.dat")

wopen("NRes400.dat")

objectvar Simuls
    Simuls = new Simul( )

for i=1,2000 {
    for j=0,74 {
        Simuls.add_stim( f1.scanvar(), j , f2.scanvar() )
    }
    Simuls.run()
    print "Simulation ", i "complete"
}

f1.ropen()
f2.ropen()

wopen()