// procedure to update stimulated synapses for burst of stimuli
//
// INPUT:
// $o1: Vector.  The vector of synapses to update.
// $2: numeric.  random seed
// $3: numeric.  release probability for first stim
// $o4: Vector.  normalised Pr
// $o5: Shape object
//
// local variables:
// N_synapses: int.     maximal number of activated synapses
// index1: int.         first index of newly activated synapses
// no_syn: int.         number of synapses activated given probability of respective combination and maximal number of activated synapses
// SynAct: Vector.      Vector of number of activated synapses on each pulse
// RelPr: Vector.       STP corrected release probabilities

obfunc set_gluSyn() {local ii,jj,N_synapses localobj SynAct, RelPr, randGen
    SynAct = new Vector(5,0) // number of activated synapses per stim
    RelPr = new Vector(5) // number of activated synapses per stim
    RelPr = $o4.c.mul($3) // release probability for each stimulus
    N_synapses = $o1.size(0)    
    randGen = new Random($2)
    
    //print "RelPr.x[4] is ", RelPr.x[$o4.size(0)-1]
    for jj=1,stim_no{
        //print jj
        for ii=1,N_synapses {
            //print ii
            //print $o1.x[(ii-1)]*stim_no+jj-1
            if (randGen.uniform(0,1)<RelPr.x[jj-1]){
                ncAmpa[$o1.x[(ii-1)]*stim_no+jj-1].weight = ampaWeight
                ncAmpa[$o1.x[(ii-1)]*stim_no+jj-1].delay = 0
                //print $o1.x[(ii-1)]*stim_no+jj-1
                ncNmda[$o1.x[(ii-1)]*stim_no+jj-1].weight = nmdaWeight
                ncNmda[$o1.x[(ii-1)]*stim_no+jj-1].delay = 0
                $o5.point_mark(synAmpa[$o1.x[ii-1]],2,"O",6)
                SynAct.x[jj-1] =SynAct.x[jj-1]+1
            } 
        }  	
    }
    return SynAct
}

obfunc set_InhSyn() {local ii,jj,N_synapses localobj SynAct, RelPr, randGen
    SynAct = new Vector(5,0) // number of activated synapses per stim
    RelPr = new Vector(5) // number of activated synapses per stim
    RelPr = $o4.c.mul($3) // release probability for each stimulus
    N_synapses = $o1.size(0)    
    randGen = new Random($2)
    
    print "RelPr.x[4] is ", RelPr.x[$o4.size(0)-1]
    for jj=1,stim_no{
        //print jj
        for ii=1,N_synapses {
            //print ii
            //print $o1.x[(ii-1)]*stim_no+jj-1
            if (randGen.uniform(0,1)<RelPr.x[jj-1]){
                ncGABA[$o1.x[(ii-1)]*stim_no+jj-1].weight = GABAweight0
                ncGABA[$o1.x[(ii-1)]*stim_no+jj-1].delay = 0
                ncGABAB[$o1.x[(ii-1)]*stim_no+jj-1].weight = GABA_release_weight
                ncGABAB[$o1.x[(ii-1)]*stim_no+jj-1].delay = 0
                //print $o1.x[(ii-1)]*stim_no+jj-1
                ncGABArect[$o1.x[(ii-1)]*stim_no+jj-1].weight = GABAweight1
                ncGABArect[$o1.x[(ii-1)]*stim_no+jj-1].delay = 0
                $o5.point_mark(synGABA[$o1.x[ii-1]],3,"O",6)
                SynAct.x[jj-1] =SynAct.x[jj-1]+1
            } 
        }  	
    }
    return SynAct
}


// INPUT:
// $o1: Vector.  The vector of synapses to update.
// $2: numeric.  random seed
// $3: numeric.  release probability for first stim
// $o4: Vector.  normalised Pr
// $o5: Shape object
//
// local variables:
// N_synapses: int.     maximal number of activated synapses
// index1: int.         first index of newly activated synapses
// no_syn: int.         number of synapses activated given probability of respective combination and maximal number of activated synapses
// SynAct: Vector.      Vector of number of activated synapses on each pulse
// RelPr: Vector.       STP corrected release probabilities

obfunc set_gluSyn_fixed_N() {local ii,jj,N_synapses localobj SynAct, N_inputs, randGen, N_output
    
    //SynAct = new Vector(5,0) , number of activated synapses per stim
    N_inputs = new Vector(5) // number of activated synapses per stim
    N_output = new Vector(5)
    N_synapses = $o1.size(0)
    N_inputs = $o4.c.mul($3).c.mul(N_synapses) // release probability for each stimulus times number of synapses available   
    //randGen = new Random($2)
    //print N_inputs.x[0], N_inputs.x[1], N_inputs.x[2], N_inputs.x[3], N_inputs.x[4]
    //print "RelPr.x[4] is ", RelPr.x[$o4.size(0)-1]
    for jj=1,stim_no{
        if (N_inputs.x[jj-1]%1>0){
            N_inputs.x[jj-1]=N_inputs.x[jj-1]-N_inputs.x[jj-1]%1
            //print N_inputs.x[jj-1]
        }
        N_output.x[jj-1]=N_inputs.x[jj-1]
        SynAct=sampleNoReplace($o1,N_inputs.x[jj-1],$2+jj) 
        //print jj
        for ii=1,N_inputs.x[jj-1] {
            //print ii
            //print $o1.x[(ii-1)]*stim_no+jj-1
            ncAmpa[SynAct.x[(ii-1)]*stim_no+jj-1].weight = ampaWeight
            ncAmpa[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            //print $o1.x[(ii-1)]*stim_no+jj-1
            ncNmda[SynAct.x[(ii-1)]*stim_no+jj-1].weight = nmdaWeight
            ncNmda[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            $o5.point_mark(synAmpa[$o1.x[ii-1]],2,"O",6)
        }  	
    }
    return N_output
}

obfunc set_InhSyn_fixed_N() {local ii,jj,N_synapses localobj SynAct, N_inputs, randGen, N_output
    
    //SynAct = new Vector(5,0) , number of activated synapses per stim
    N_inputs = new Vector(5) // number of activated synapses per stim
    N_output = new Vector(5)
    N_synapses = $o1.size(0)
    N_inputs = $o4.c.mul($3).c.mul(N_synapses) // release probability for each stimulus times number of synapses available   
    //randGen = new Random($2)
    //print N_inputs.x[0], N_inputs.x[1], N_inputs.x[2], N_inputs.x[3], N_inputs.x[4]
    //print "RelPr.x[4] is ", RelPr.x[$o4.size(0)-1]
    for jj=1,stim_no{
        if (N_inputs.x[jj-1]%1>0){
            N_inputs.x[jj-1]=N_inputs.x[jj-1]-N_inputs.x[jj-1]%1
            //print N_inputs.x[jj-1]
        }
        N_output.x[jj-1]=N_inputs.x[jj-1]
        SynAct=sampleNoReplace($o1,N_inputs.x[jj-1],$2+jj) 
        //print jj
        for ii=1,N_inputs.x[jj-1] {
            ncGABA[SynAct.x[(ii-1)]*stim_no+jj-1].weight = GABAweight0
            ncGABA[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            ncGABAB[SynAct.x[(ii-1)]*stim_no+jj-1].weight = GABA_release_weight
            ncGABAB[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            //print $o1.x[(ii-1)]*stim_no+jj-1
            ncGABArect[SynAct.x[(ii-1)]*stim_no+jj-1].weight = GABAweight1
            ncGABArect[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            $o5.point_mark(synGABA[SynAct.x[ii-1]],3,"O",6)
        }  	
    }
    return N_output
}

// INPUT:
// $o1: Vector.  The vector of synapses to update.
// $2: numeric.  random seed
// $3: numeric.  number of synapses
// $o4: Shape object
//
// local variables:
// N_synapses: int.     maximal number of activated synapses
// index1: int.         first index of newly activated synapses
// no_syn: int.         number of synapses activated given probability of respective combination and maximal number of activated synapses
// SynAct: Vector.      Vector of number of activated synapses on each pulse
// RelPr: Vector.       STP corrected release probabilities

obfunc set_InhSyn_syn_fixed() {local ii,jj,N_synapses localobj SynAct, randGen, N_output
    
    //SynAct = new Vector(5,0) , number of activated synapses per stim
    N_output = new Vector(5)
    N_synapses = $o1.size(0)
    N_inputs = $3   // release probability for each stimulus times number of synapses available   
    SynAct=sampleNoReplace($o1,N_inputs,$2)
    for jj=1,stim_no{ 
    //print jj
        for ii=1,N_inputs{
        //print ii
            ncGABA[SynAct.x[(ii-1)]*stim_no+jj-1].weight = GABAweight0
            ncGABA[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            ncGABAB[SynAct.x[(ii-1)]*stim_no+jj-1].weight = GABA_release_weight
            ncGABAB[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
            //print $o1.x[(ii-1)]*stim_no+jj-1
            ncGABArect[SynAct.x[(ii-1)]*stim_no+jj-1].weight = GABAweight1
            ncGABArect[SynAct.x[(ii-1)]*stim_no+jj-1].delay = 0
        }
        N_output.x[jj-1] = N_inputs
    }
    //print "done"
    for ii=1,N_inputs{ $o4.point_mark(synGABA[SynAct.x[ii-1]],3,"O",6)}
    return N_output
}