// THIS FILE ALLOCATES EXCITATORY SYNAPSES ACROSS THE ARBOR.

// Preallocate objects for excitation.
// Prep for synapse placement.
nExc = 30000
objref synAmpa[nExc] //nsAmpa[nExc],ncAmpa[nExc],
objref synNmda[nExc] //nsNmda[nExc],ncNmda[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)
								
								synNmda[synCur-1] = new Exp2SynNmda(x)
							}
				
						}else{
							toAdd = 0
						}
						exc_syns(x) = toAdd
					}
				}
			}
		}
		

	}
	
	return synCur-$3
}




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

// Add synapses to basal dendrites.
	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 //0.5 // Andrasfalvy and Magee, 2001; cited in Migliore 2003// 0.2  // Jarsky et al., 2005
	synAmpa[ii-1].tau2 = 3 // Andrasfalvy and Magee, 2001; cited in Migliore 2003// 2 // Jarsky et al., 2005
	synAmpa[ii-1].e = 0
	synNmda[ii-1].tau1= nmdaTau1 // Schulz; 5  
	synNmda[ii-1].tau2 = nmdaTau2 // Schulz; 100 
	synNmda[ii-1].e = 0
	
}