load_file("nrngui.hoc")
cvode_active(0)
load_file("geo5038804.hoc")

a1=0.00242
b1=0.0157

a1max=0.1
b1max=0.48

rap=5

seed=1

peso=11.0

dist=300 

numbasal  = 52
numapical = 70
numuser5  = 49

objref stim0[100], syn0[100], nc0[100], rn2
objref syn1[13]
objref nc1[13]
objref stim1[13], f[100]

//***********************************************
objref datafile, data1
datafile = new File("Napical.txt")
strdef string1
datafile.ropen()
nrrow=0
while (datafile.gets(string1)>-1){
nrrow=nrrow+1
}
datafile.close()

datafile.ropen()
data1 = new Matrix()
data1.scanf(datafile,nrrow,4)
datafile.close()
ll=0
//****************************************************************


load_file("biophys.hoc")

load_file("efheader.hoc")

access soma
freq=50
distance()

tstop=4000

// noise 

for kk=0, numuser5-1   user5[kk] if(distance(0.5) < dist){  // cell-dependent
	f[kk] = new Gfluct2(0.5)
	f[kk].g_e0 = a1
	f[kk].g_i0 = b1
	f[kk].std_e = a1/rap
	f[kk].std_i = b1/rap
	f[kk].new_seed(seed)
	print " noise #",kk, " at ",secname(), " distance  ", distance(0.5), seed
}


proc init() {
t=0
seed=1
for kk=0, numuser5-1   user5[kk] if(distance(0.5) < dist){  // cell-dependent
	f[kk].new_seed(seed)
}

  finitialize(Vrest)

	forall {
		v = Vrest
		if(ismembrane("nax") || ismembrane("na3")) {
			ena=55
		}
		if(ismembrane("kdr") || ismembrane("kap") || ismembrane("kad")) {
			ek=-90
		}
		if(ismembrane("hd")) {
			ehd_hd=-30
		}
	}

	finitialize(Vrest)
	fcurrent()

	forall {
		for(x) {
			if(ismembrane("na3") || ismembrane("nax")) {
				e_pas(x)=v(x)+(ina(x)+ik(x))/g_pas(x)
			}
			if(ismembrane("hd")) {
				e_pas(x)=e_pas(x)+i_hd(x)/g_pas(x)
			}
		}
	}


}

inter=20
soma {
	for j=0,12 {
	stim1[j]= new NetStim(.5)
	stim1[j].number=100000
	stim1[j].interval=inter
	stim1[j].start=10
	stim1[j].noise=1
	}	
}
	
// definition of config number: k

for k=5,5{
ll=0+13*k
ii=0
for jj=ll+0,ll+12{

    apical_dendrite[data1.x[jj][3]]{
	syn1[ii]= new Exp2Syn(.1) 
	syn1[ii].tau1=0.5
	syn1[ii].tau2=5
	syn1[ii].e=0
	
print data1.x[jj][3]
	}
ii=ii+1
}

}

for kk=0,12{
		
		nc1[kk]= new NetCon(stim1[kk], syn1[kk], 0, 0, peso*1e-3)	
	}

load_file("voltage.ses")
objref vp
vp = save_window_
vp.exec_menu("Keep Lines")
proc sim() {
  vp.erase_all()

	vp.label(0.949956, 0.931495, "NO EF", 2, 1, 0, 0, 1)
	vp.label(0.950834, 0.878114, "EF", 2, 1, 0, 0, 6)

	vp.brush(1)
	vp.color(col)
	vp.addvar("", "v(0.5)")
	setstim(400, 12000, 50, 50, field)
	changefield(100, 90, 90)
	run()

}

xpanel(" ")
xlabel("  ")
xradiobutton("NO EF","setNOFIELD()")
xradiobutton("EF","setFIELD()")
xlabel("  ")
xbutton("Run simulation", "sim()")
xlabel("  ")
xpanel()


proc setNOFIELD() {
field=0
col=1
}

proc setFIELD() {
field=40
col=2
}