// synapses on apical dendrites

objref syn_, ns_, nc_, AMPA_synlist, AMPA_nslist, AMPA_nclist
AMPA_synlist=new List()
AMPA_nslist=new List()
AMPA_nclist=new List()

proc syns(){
	total_AMPA=0
	no_of_AMPA=0
	forsec apical_list {
		for(x) if(x > 0 && x < 1 && distance(x)>100) { // synapses distributed from 100 um from soma
			no_of_AMPA = int(area(x)*1/100) // synapses distributed per um^2
			AMPA_syn(x,no_of_AMPA)
			total_AMPA+=no_of_AMPA
		}
	}
}

proc AMPA_syn() {local i
	syn_ = new Exp2Syn($1)  AMPA_synlist.append(syn_)
		syn_.e = 0
		syn_.tau1 = .5
		syn_.tau2 = 3

	for i=0, $2-1{
		ns_nc(AMPA_nslist,syn_,AMPA_nclist)
	}
}

proc ns_nc(){
		bar ns_ = new NetStim(.5) $o1.append(ns_)
			ns_.interval = 100
			ns_.number = 5
			ns_.start = 10
			ns_.noise = 0

		nc_ = new NetCon(ns_,$o2) $o3.append(nc_)
			nc_.weight = 0.0003 // Maximal conductances in uS
			nc_.delay = 0
    		nc_.threshold = 0
}


AMPA_rate	= 20	// Firing rates in Hz
ns_frac  	= 0.3	// fraction of synapses activated

proc ns_stim() {
	if (ns_frac<=0) ns_frac = 1e-5
	for i=0, AMPA_nslist.count()-1 {AMPA_nslist.object[i].number = 0}
	for(i=0; i <= AMPA_nslist.count() - 1; i+=1/ns_frac) {
		AMPA_nslist.object[i].number = 5
	}
	for i=0, AMPA_nslist.count() - 1 {
		AMPA_nslist.object[i].start = 200
		AMPA_nslist.object[i].noise = 0
		AMPA_nslist.object[i].interval=1000/AMPA_rate
	}
}

syns()

ns_stim(AMPA_nslist,AMPA_rate)


// graphs

objref plts
plts = new Shape(0)
plts.view(-230, -600, 1665, 920, 20, 120, 360, 200)

objref vg
vg = new Graph(0)
vg.view(0, -70, 800, 20, 536, 122, 300, 200)
graphList[0].append(vg)
vg.exec_menu("Keep Lines")

proc gui(){
	xpanel("Simulation")
		xbutton("regular - 20 Hz","reg_20()")
		xbutton("regular - 50 Hz","reg_50()")
		xbutton("burster - 20 Hz","burst_20()")
		xbutton("burster - 50 Hz","burst_50()")
		xbutton("clear graph","vg.erase_all()")
	xpanel(1000,100)
}


proc reg_20(){
	vg.addvar("regular - 20 Hz","v(.5)", 1, 1, 0.7, 0.9, 2)
	ghd=0.45e-5
	h_density()
	AMPA_rate=20
	ns_stim()
	run()
}

proc reg_50(){
	vg.addvar("regular - 50 Hz","v(.5)", 2, 1, 0.7, 0.9, 2)
	ghd=0.45e-5
	h_density()
	AMPA_rate=50
	ns_stim()
	run()
}

proc burst_20(){
	vg.addvar("burster - 20 Hz","v(.5)", 3, 1, 0.7, 0.9, 2)
	ghd=1.2e-5
	h_density()
	AMPA_rate=20
	ns_stim()
	run()
}

proc burst_50(){
	vg.addvar("burster - 50 Hz","v(.5)", 4, 1, 0.7, 0.9, 2)
	ghd=1.2e-5
	h_density()
	AMPA_rate=50
	ns_stim()
	run()
}

gui()