// THIS FILE ALLOCATES EXCITATORY SYNAPSES ACROSS THE ARBOR.

// Preallocate objects for excitation.
// Prep for synapse placement.
nExc = 100000
objref nsAmpa[nExc],ncAmpa[nExc],synAmpa[nExc]
objref nsNmda[nExc],ncNmda[nExc],synNmda[nExc]
synInd = 0

// This function takes a SectionRef and a vector with surface area synapse
// scaling and places the number of correspond excitatory synapses.  Note that
// the low index of the first placed synapse needs to be specified in order to
// give indices to all of the generated synapses.
//
// $o1: SectionRef instance.  Contains the section to implement with the
// 	scaling rule.
// $o2: Vector instance.  Contains the scaling rule to implement.  If the
//	density is uniform across the branch (e.g., 0.1 synapses/um2), then
//	simply supply as 
//		foo = new Vector(1); foo.x[0] = 0.1; $o1 = foo
// 	Conversely, if
// 	there is spatial variation across the branch (say, 0.1 synapses/um2 in the
//	first third, 0.2 synapses/um2 in the second third, and 0.3 synapses/um2
// 	in the final third), then, supply as
//		foo = new Vector(3) ; foo.x[0] = 0.1, foo.x[1] = 0.2;
//			foo.x[2] = 0.3 ; $o1 = foo
// $3: numeric.  The index of the first synapse to be created and placed.
//
// The number of synapses added is returned.
func addExcitation() {local nDiv,runningSa,synAdd,synCur,ii,jj
	nDiv = $o2.size()
	synCur = $3
	// Place synapses.
	$o1.sec {
		for ii=1,nDiv {
	
			runningSa=0
			synAdd=0
			
			for(x,0){
				if(((ii-1)/nDiv)<x_eff(x)){
					if(x_eff(x)<(ii/nDiv+0.00000001)){
						runningSa+=area(x)
						if(int(runningSa*$o2.x[ii-1])>(synAdd+0.000001)){
							toAdd = int(runningSa*$o2.x[ii-1]-synAdd)

							for jj=1,toAdd{	
								synCur = synCur+1
								synAdd = synAdd+1
								synAmpa[synCur-1] = new excSyn(x)
								synAmpa[synCur-1].xEff = x_eff(x)
								nsAmpa[synCur-1] = new NetStim(x)
								ncAmpa[synCur-1] = new NetCon(nsAmpa[synCur-1],synAmpa[synCur-1])
	
								synNmda[synCur-1] = new Exp2SynNmda(x)
								nsNmda[synCur-1] = new NetStim(x)
								ncNmda[synCur-1] = new NetCon(nsNmda[synCur-1],synNmda[synCur-1])
							}
				
						}else{
							toAdd = 0
						}
						exc_syns(x) = toAdd
					}
				}
			}
		}
		

	}
	
	return synCur-$3
}




// Add synapses to tuft.
	objref denExcTuft
	denExcTuft = new Vector(1)
		denExcTuft.x[0] = 0.2
		
	forsec tuftList {
		curSec = new SectionRef()
		numAdded = addExcitation(curSec,denExcTuft,synInd)	
		synInd+=numAdded
	}
	
// Add synapses to obliques.
	objref denExcObl
	denExcObl = new Vector(1)
		denExcObl.x[0] = 1.2 // spines/um2
		
	forsec obliqueList {
		curSec = new SectionRef()	
		numAdded = addExcitation(curSec,denExcObl,synInd)
		synInd+=numAdded
	}
	
// Add synapses to trunk.
	objref denExcTrunk,denExcTrunkTemp
	denExcTrunk = new Vector(1)
		denExcTrunk.x[0] = 0.8
	denExcTrunkTemp = new Vector(denExcTrunk.size())
	soma.sec{distance()}
	
	forsec primList {
		curSec = new SectionRef()	
		scaleFact = theDist/200
		denExcTrunkTemp.copy(denExcTrunk)
		denExcTrunkTemp.mul(scaleFact)
		
		numAdded = addExcitation(curSec,denExcTrunkTemp,synInd)
		synInd+=numAdded
	}

// Add synapses to obliques.
	objref denExcBasalPrim,denExcBasalSec,denExcBasalTerm
	denExcBasalPrim = new Vector(1)
		denExcBasalPrim.x[0] = 0
	denExcBasalSec = new Vector(1)
		denExcBasalSec.x[0] = 0.1
	denExcBasalTerm = new Vector(1)
		denExcBasalTerm.x[0] = 0.9
	
		
	forsec basalList {
		curSec = new SectionRef()
		if(isTerm_id){
			numAdded = addExcitation(curSec,denExcBasalTerm,synInd)
		}else{
			if(abs(brOrd_id-1)<0.001){
				numAdded = addExcitation(curSec,denExcBasalPrim,synInd)
			}else{
				if(abs(brOrd_id-2)<0.001){
					numAdded = addExcitation(curSec,denExcBasalSec,synInd)
				}else{
					numAdded = addExcitation(curSec,denExcBasalSec,synInd) // keep as 2ary for time being
				}
			}
		}

		synInd+=numAdded
	}
	
// reset value of nExc
nExc = synInd	
//print "Number of excitatory synapses is: ",nExc


// Change properties of excitatory inputs.
for ii=1,nExc {

	synAmpa[ii-1].tau1= 0.2  // Jarsky et al., 2005
	synAmpa[ii-1].tau2 = 2 // Jarsky et al., 2005
	synAmpa[ii-1].e = 0
	
	
	nsAmpa[ii-1].interval = 0
	nsAmpa[ii-1].number = 1
	nsAmpa[ii-1].start = 97
	nsAmpa[ii-1].noise = 0
	ncAmpa[ii-1].weight = 0.00018 // 0.18 nS; from Jarsky et al 2005
	ncAmpa[ii-1].delay = 0
	
	synNmda[ii-1].tau1= 5  
	synNmda[ii-1].tau2 = 100 
	synNmda[ii-1].e = 0
	
	
	nsNmda[ii-1].interval = 0
	nsNmda[ii-1].number = 1
	nsNmda[ii-1].start = 97
	nsNmda[ii-1].noise = 0
	ncNmda[ii-1].weight = 0.00018
	ncNmda[ii-1].delay = 0


}