// File for generating gap junctions and synapses
load_file("Cell_positions.hoc")     // File for generating presynaptic network cell positions
load_file("create_Cells.hoc")
load_file("read_population.hoc")
load_file("Gap.tem")
load_file("AII_ONBCGap.tem")

// Objrefs
objref PR_Gap_Junctions[MaxNumPRGapConnections]
objref AII_Gap_Junctions[MaxNumAIIGapConnections]
objref AmaGapJunctions[MaxNumAmaGapConnections]

objref BIP_upper_Dendritic_Field_Radius, BIP_lower_Dendritic_Field_Radius
objref HZ_upper_Dendritic_Field_Radius, HZ_lower_Dendritic_Field_Radius
objref AII_upper_Dendritic_Field_Radius, AII_lower_Dendritic_Field_Radius

objref num_PR_2_BIP_upper, num_PR_2_BIP_lower
objref num_PR_2_HZ_upper, num_PR_2_HZ_lower
objref num_RBC_2_AII_upper, num_RBC_2_AII_lower, num_ONBC_2_AII_upper, num_ONBC_2_AII_lower
objref num_AII_2_OFFBC_upper, num_AII_2_OFFBC_lower, num_AII_2_OFF_RGC_upper, num_AII_2_OFF_RGC_lower
objref num_BIP_2_RGC, num_BIPSyn_2_RGC, total_RGC_syn

// Create the cell arrays
generate_PR_array()
generate_HZ_array()
generate_BIP_array()
generate_AII_array()
generate_RGC_array()

// objrefs for vertical synapses
    // PR Synapses
objref syn_PR_upper_2_BIP_upper[PR_total_upper_cells][BIP_total_upper_cells], syn_PR_lower_2_BIP_upper[PR_total_lower_cells][BIP_total_upper_cells]
objref syn_PR_upper_2_BIP_lower[PR_total_upper_cells][BIP_total_lower_cells], syn_PR_lower_2_BIP_lower[PR_total_lower_cells][BIP_total_lower_cells]
    // BIP Synapses
objref syn_BIP_upper_2_ONRGC[BIP_total_upper_cells][num_gang][maxNumDendConnec], syn_BIP_lower_2_ONRGC[BIP_total_lower_cells][num_gang][maxNumDendConnec]
objref syn_BIP_upper_2_OFFRGC[BIP_total_upper_cells][num_gang][maxNumDendConnec], syn_BIP_lower_2_OFFRGC[BIP_total_lower_cells][num_gang][maxNumDendConnec]
objref random_connec

// objrefs for lateral synapses
    // HZ Synapses
objref pc1
objref syn_PR_upper_2_HZ_upper[PR_total_upper_cells][HZ_total_upper_cells], syn_PR_lower_2_HZ_upper[PR_total_lower_cells][HZ_total_upper_cells]
objref syn_PR_upper_2_HZ_lower[PR_total_upper_cells][HZ_total_lower_cells], syn_PR_lower_2_HZ_lower[PR_total_lower_cells][HZ_total_lower_cells]
objref syn_HZ_upper_2_PR_upper[HZ_total_upper_cells][PR_total_upper_cells], syn_HZ_lower_2_PR_upper[HZ_total_lower_cells][PR_total_upper_cells]
objref syn_HZ_upper_2_PR_lower[HZ_total_upper_cells][PR_total_lower_cells], syn_HZ_lower_2_PR_lower[HZ_total_lower_cells][PR_total_lower_cells]
    // Rod BIP 2 AII Synapses
objref syn_BIP_upper_2_AII_upper[BIP_total_upper_cells][AII_total_upper_cells], syn_BIP_lower_2_AII_upper[BIP_total_lower_cells][AII_total_upper_cells]
objref syn_BIP_upper_2_AII_lower[BIP_total_upper_cells][AII_total_lower_cells], syn_BIP_lower_2_AII_lower[BIP_total_lower_cells][AII_total_lower_cells]
    // AII Synapses
objref syn_AII_upper_2_BIP_upper[AII_total_upper_cells][BIP_total_upper_cells], syn_AII_lower_2_BIP_upper[AII_total_lower_cells][BIP_total_upper_cells]
objref syn_AII_upper_2_BIP_lower[AII_total_upper_cells][BIP_total_lower_cells], syn_AII_lower_2_BIP_lower[AII_total_lower_cells][BIP_total_lower_cells]
objref syn_AII_upper_2_RGC[AII_total_upper_cells][num_gang][maxNumDendConnec], syn_AII_lower_2_RGC[AII_total_lower_cells][num_gang][maxNumDendConnec]

// objrefs for initial vclamp
objref vc_PR_upper[PR_total_upper_cells], vc_PR_lower[PR_total_lower_cells]
objref vc_HZ_upper[HZ_total_upper_cells], vc_HZ_lower[HZ_total_lower_cells]
objref vc_BIP_upper[BIP_total_upper_cells], vc_BIP_lower[BIP_total_lower_cells]
objref vc_AII_upper[AII_total_upper_cells], vc_AII_lower[AII_total_lower_cells]


proc generate_PR_Gap_Junctions() {

    // PR GAP JUNCTIONS
    // Check across the diagonal
    for k = 0, PR_total_upper_cells - 1 {
        for l = 0, PR_total_lower_cells - 1 {
            // check if index values have a difference of 1, meaning next to each other
            if(abs(PR_upper_index.x[k][0] - PR_lower_index.x[l][0]) <= 1){
                // Check if distance between 2 cells is < 1.5x the cell spacing
                Distance_between_cells = sqrt((PR_upper_positions.x[k][0] - PR_lower_positions.x[l][0])^2 + (PR_upper_positions.x[k][1] - PR_lower_positions.x[l][1])^2)
                calc_cell_density(PR_upper_positions.x[k][0]/1000, PR_upper_positions.x[k][1]/1000, 0, 1)       // Function takes units in mm
                calc_cell_spacing()
                if(Distance_between_cells < (sqrt((cell_spacing_x + cell_spacing_y)^2))*1000*1.5) {
                    PR_Gap_Junctions[NumPRGapConnections] = new Gap()
                    PR_upper[k].soma PR_Gap_Junctions[NumPRGapConnections].src(0.95)
                    PR_lower[l].soma PR_Gap_Junctions[NumPRGapConnections].target(0.95)
                    // Check type of cell is the same
                    if(PR_upper_cell_type.x[k][0] == PR_lower_cell_type.x[l][0]) {
                        PR_Gap_Junctions[NumPRGapConnections].g(HomoPRGap)
                        NumPRGapConnections = NumPRGapConnections + 1
                    } else {
                        PR_Gap_Junctions[NumPRGapConnections].g(HeteroPRGap)
                        NumPRGapConnections = NumPRGapConnections + 1
                    }
                }
            }
        }
    }

    // Check upper half
    for k = 0, PR_total_upper_cells - 1 {
        for l = 0, PR_total_upper_cells - 1 {
            if(k != l && l > k) {
                Distance_between_cells = sqrt((PR_upper_positions.x[k][0] - PR_upper_positions.x[l][0])^2 + (PR_upper_positions.x[k][1] - PR_upper_positions.x[l][1])^2)
                calc_cell_density(PR_upper_positions.x[k][0]/1000, PR_upper_positions.x[k][1]/1000, 0, 1)       // Function takes units in mm
                calc_cell_spacing()
                if(Distance_between_cells < (sqrt((cell_spacing_x + cell_spacing_y)^2))*1000*1.5) {
                    if(abs(PR_upper_index.x[k][0] - PR_upper_index.x[l][0]) <= 1) {
                        PR_Gap_Junctions[NumPRGapConnections] = new Gap()
                        PR_upper[k].soma PR_Gap_Junctions[NumPRGapConnections].src(0.95)
                        PR_upper[l].soma PR_Gap_Junctions[NumPRGapConnections].target(0.95)
                        if(PR_upper_cell_type.x[k][0] == PR_upper_cell_type.x[k][0]) {
                            PR_Gap_Junctions[NumPRGapConnections].g(HomoPRGap)
                            NumPRGapConnections = NumPRGapConnections + 1
                        } else {
                            PR_Gap_Junctions[NumPRGapConnections].g(HeteroPRGap)
                            NumPRGapConnections = NumPRGapConnections + 1
                        }
                    }
                }
            }
        }
    }

    // Check lower half
    for k = 0, PR_total_lower_cells - 1 {
        for l = 0, PR_total_lower_cells - 1 {
            if(k != l && l > k) {
                Distance_between_cells = sqrt((PR_lower_positions.x[k][0] - PR_lower_positions.x[l][0])^2 + (PR_lower_positions.x[k][1] - PR_lower_positions.x[l][1])^2)
                calc_cell_density(PR_lower_positions.x[k][0]/1000, PR_lower_positions.x[k][1]/1000, 0, 1)       // Function takes units in mm
                calc_cell_spacing()
                if(Distance_between_cells < (sqrt((cell_spacing_x + cell_spacing_y)^2))*1000*1.5) {
                    if(abs(PR_lower_index.x[k][0] - PR_lower_index.x[l][0]) <= 1) {
                        PR_Gap_Junctions[NumPRGapConnections] = new Gap()
                        PR_lower[k].soma PR_Gap_Junctions[NumPRGapConnections].src(0.95)
                        PR_lower[l].soma PR_Gap_Junctions[NumPRGapConnections].target(0.95)
                        if(PR_lower_cell_type.x[k][0] == PR_lower_cell_type.x[k][0]) {
                            PR_Gap_Junctions[NumPRGapConnections].g(HomoPRGap)
                            NumPRGapConnections = NumPRGapConnections + 1
                        } else {
                            PR_Gap_Junctions[NumPRGapConnections].g(HeteroPRGap)
                            NumPRGapConnections = NumPRGapConnections + 1
                        }
                    }
                }
            }
        }
    }

}

proc generate_AII_gap_junctions() {

    // AII-AII homocellular gap junctions
    // Check diagonal
    for k = 0, AII_total_upper_cells - 1 {
        for l = 0, AII_total_lower_cells - 1 {
            // check if index values have a difference of 1, meaning next to each other
            if(abs(AII_upper_index.x[k][0] - AII_lower_index.x[l][0]) <= 1) {
                Distance_between_cells = sqrt((AII_upper_positions.x[k][0] - AII_lower_positions.x[l][0])^2 + (AII_upper_positions.x[k][1] - AII_lower_positions.x[l][1])^2)
                calc_cell_density(AII_upper_positions.x[k][0]/1000, AII_upper_positions.x[k][1]/1000, 0, 4)       // Function takes units in mm
                calc_cell_spacing()
                if(Distance_between_cells < (sqrt((cell_spacing_x + cell_spacing_y)^2))*1000*1.5) {
                    AII_Gap_Junctions[NumAIIGapConnections] = new Gap()
                    AII_upper[k].soma AII_Gap_Junctions[NumAIIGapConnections].src(0.95)
                    AII_lower[l].soma AII_Gap_Junctions[NumAIIGapConnections].target(0.95)
                    AII_Gap_Junctions[NumAIIGapConnections].g(HomoAIIGap)
                    NumAIIGapConnections = NumAIIGapConnections + 1
                }
            }
        }
    }

    // Check upper half
    for k = 0, AII_total_upper_cells - 1 {
        for l = 0, AII_total_upper_cells - 1 {
            if(k != l && l > k) {
                if(abs(AII_upper_index.x[k][0] - AII_upper_index.x[l][0]) <= 1) {
                    Distance_between_cells = sqrt((AII_upper_positions.x[k][0] - AII_upper_positions.x[l][0])^2 + (AII_upper_positions.x[k][1] - AII_upper_positions.x[l][1])^2)
                    calc_cell_density(AII_upper_positions.x[k][0]/1000, AII_upper_positions.x[k][1]/1000, 0, 4)       // Function takes units in mm
                    calc_cell_spacing()
                    if(Distance_between_cells < (sqrt((cell_spacing_x + cell_spacing_y)^2))*1000*1.5) {
                        AII_Gap_Junctions[NumAIIGapConnections] = new Gap()
                        AII_upper[k].soma AII_Gap_Junctions[NumAIIGapConnections].src(0.95)
                        AII_upper[l].soma AII_Gap_Junctions[NumAIIGapConnections].target(0.95)
                        AII_Gap_Junctions[NumAIIGapConnections].g(HomoAIIGap)
                        NumAIIGapConnections = NumAIIGapConnections + 1
                    }
                }
            }
        }
    }

    // Check lower half
    for k = 0, AII_total_lower_cells - 1 {
        for l = 0, AII_total_lower_cells - 1 {
            if(k != l && l > k) {
                if(abs(AII_lower_index.x[k][0] - AII_lower_index.x[l][0]) <= 1) {
                    Distance_between_cells = sqrt((AII_lower_positions.x[k][0] - AII_lower_positions.x[l][0])^2 + (AII_lower_positions.x[k][1] - AII_lower_positions.x[l][1])^2)
                    calc_cell_density(AII_lower_positions.x[k][0]/1000, AII_lower_positions.x[k][1]/1000, 0, 4)       // Function takes units in mm
                    calc_cell_spacing()
                    if(Distance_between_cells < (sqrt((cell_spacing_x + cell_spacing_y)^2))*1000*1.5) {
                        AII_Gap_Junctions[NumAIIGapConnections] = new Gap()
                        AII_lower[k].soma AII_Gap_Junctions[NumAIIGapConnections].src(0.95)
                        AII_lower[l].soma AII_Gap_Junctions[NumAIIGapConnections].target(0.95)
                        AII_Gap_Junctions[NumAIIGapConnections].g(HomoAIIGap)
                        NumAIIGapConnections = NumAIIGapConnections + 1
                    }
                }
            }
        }
    }

}

proc generate_dendritic_fields() {

    // Calculate Dendritic Field Sizes
    BIP_upper_Dendritic_Field_Radius = new Vector(BIP_total_upper_cells)
    BIP_lower_Dendritic_Field_Radius = new Vector(BIP_total_lower_cells)
    HZ_upper_Dendritic_Field_Radius = new Vector(HZ_total_upper_cells)
    HZ_lower_Dendritic_Field_Radius = new Vector(HZ_total_lower_cells)
    AII_upper_Dendritic_Field_Radius = new Vector(AII_total_upper_cells)
    AII_lower_Dendritic_Field_Radius = new Vector(AII_total_lower_cells)

    for i = 0, BIP_total_upper_cells - 1 {
        // Calculate the size of the dendritic field based on the "touch thy neighbour rule" (Lee 2019)
        calc_cell_density(BIP_upper_positions.x[i][0]/1000, BIP_upper_positions.x[i][1]/1000, 0, 3)       // Function takes units in mm
        calc_cell_spacing()
        BIP_upper_Dendritic_Field_Radius.x[i] = (sqrt((cell_spacing_x^2 + cell_spacing_y^2)))*1000
    }

    for i = 0, BIP_total_lower_cells - 1 {
        calc_cell_density(BIP_lower_positions.x[i][0]/1000, BIP_lower_positions.x[i][1]/1000, 0, 3)       // Function takes units in mm
        calc_cell_spacing()
        BIP_lower_Dendritic_Field_Radius.x[i] = (sqrt((cell_spacing_x^2 + cell_spacing_y^2)))*1000
    }

    for i = 0, HZ_total_upper_cells - 1 {
        calc_cell_density(HZ_upper_positions.x[i][0]/1000, HZ_upper_positions.x[i][1]/1000, 0, 2)       // Function takes units in mm
        calc_cell_spacing()
        HZ_upper_Dendritic_Field_Radius.x[i] = (sqrt((cell_spacing_x^2 + cell_spacing_y^2)))*1000
    }

    for i = 0, HZ_total_lower_cells - 1 {
        calc_cell_density(HZ_lower_positions.x[i][0]/1000, HZ_lower_positions.x[i][1]/1000, 0, 2)       // Function takes units in mm
        calc_cell_spacing()
        HZ_lower_Dendritic_Field_Radius.x[i] = (sqrt((cell_spacing_x^2 + cell_spacing_y^2)))*1000
    }

    for i = 0, AII_total_upper_cells - 1 {
        calc_cell_density(AII_upper_positions.x[i][0]/1000, AII_upper_positions.x[i][1]/1000, 0, 4)       // Function takes units in mm
        calc_cell_spacing()
        AII_upper_Dendritic_Field_Radius.x[i] = (sqrt((cell_spacing_x^2 + cell_spacing_y^2)))*1000
    }

    for i = 0, AII_total_lower_cells - 1 {
        calc_cell_density(AII_lower_positions.x[i][0]/1000, AII_lower_positions.x[i][1]/1000, 0, 4)       // Function takes units in mm
        calc_cell_spacing()
        AII_lower_Dendritic_Field_Radius.x[i] = (sqrt((cell_spacing_x^2 + cell_spacing_y^2)))*1000
    }

}

proc generate_PR2BC_Synapses() {

    // --------------------------------------------------------------------------------
    // ----------------------------------BIPOLAR CELL SYNAPSES-------------------------
    // --------------------------------------------------------------------------------

    num_PR_2_BIP_upper = new Vector(BIP_total_upper_cells)
    num_PR_2_BIP_lower = new Vector(BIP_total_lower_cells)
    // Generate synapses for the upper half of the bipolar cell array
    for i = 0, BIP_total_upper_cells - 1 {
        // ----------------------------------Rod_PR to Rod_Bip Synapses Start-----------------------------
        num_PR_2_BIP_upper.x[i] = 0
        // Check if BC is a Rod Bipolar (type = 1)
        if(BIP_upper_cell_type.x[i][0] == 1) {
            for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Rod (type = 1)
                if(PR_upper_cell_type.x[j][0] == 1) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_upper_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (BIP_upper_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_upper.x[i] = num_PR_2_BIP_upper.x[i] + 1
                        syn_PR_upper_2_BIP_upper[j][i] = new GradSyn_excite()
                        BIP_upper[i].soma syn_PR_upper_2_BIP_upper[j][i].loc(0.05)
                        setpointer syn_PR_upper_2_BIP_upper[j][i].V_pre, PR_upper[j].soma.v(0.95)
                        syn_PR_upper_2_BIP_upper[j][i].e = 0
                        syn_PR_upper_2_BIP_upper[j][i].V_thr = RodBipSynapticThreshold
                        syn_PR_upper_2_BIP_upper[j][i].g_max = RodBipSynapticConductance
                    }
                }
            }
            for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Rod (type = 1)
                if(PR_lower_cell_type.x[j][0] == 1) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_upper_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (BIP_upper_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_upper.x[i] = num_PR_2_BIP_upper.x[i] + 1
                        syn_PR_lower_2_BIP_upper[j][i] = new GradSyn_excite()
                        BIP_upper[i].soma syn_PR_lower_2_BIP_upper[j][i].loc(0.05)
                        setpointer syn_PR_lower_2_BIP_upper[j][i].V_pre, PR_lower[j].soma.v(0.95)
                        syn_PR_lower_2_BIP_upper[j][i].e = 0
                        syn_PR_lower_2_BIP_upper[j][i].V_thr = RodBipSynapticThreshold
                        syn_PR_lower_2_BIP_upper[j][i].g_max = RodBipSynapticConductance
                    }
                }
            }
        }
        // ----------------------------------Rod_PR to Rod_Bip Synapses End-----------------------------

        // ----------------------------------Cone_PR to ON_Bip Synapses Start-----------------------------
        // Check if BC is a ON Bipolar (type = 2)
        if(BIP_upper_cell_type.x[i][0] == 2) {
            for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_upper_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_upper_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (BIP_upper_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_upper.x[i] = num_PR_2_BIP_upper.x[i] + 1
                        syn_PR_upper_2_BIP_upper[j][i] = new GradSyn_excite()
                        BIP_upper[i].soma syn_PR_upper_2_BIP_upper[j][i].loc(0.05)
                        setpointer syn_PR_upper_2_BIP_upper[j][i].V_pre, PR_upper[j].soma.v(0.95)
                        syn_PR_upper_2_BIP_upper[j][i].e = 0
                        syn_PR_upper_2_BIP_upper[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_upper_2_BIP_upper[j][i].g_max = ONBipSynapticConductance
                    }
                }
            }
            for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_lower_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_upper_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (BIP_upper_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_upper.x[i] = num_PR_2_BIP_upper.x[i] + 1
                        syn_PR_lower_2_BIP_upper[j][i] = new GradSyn_excite()
                        BIP_upper[i].soma syn_PR_lower_2_BIP_upper[j][i].loc(0.05)
                        setpointer syn_PR_lower_2_BIP_upper[j][i].V_pre, PR_lower[j].soma.v(0.95)
                        syn_PR_lower_2_BIP_upper[j][i].e = 0
                        syn_PR_lower_2_BIP_upper[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_lower_2_BIP_upper[j][i].g_max = ONBipSynapticConductance
                    }
                }
            }
        }
        // ----------------------------------Cone_PR to ON_Bip Synapses end-----------------------------

        // ----------------------------------Cone_PR to OFF_Bip Synapses Start-----------------------------
        // Check if BC is a OFF Bipolar (type = 3)
        if(BIP_upper_cell_type.x[i][0] == 3) {
            for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_upper_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_upper_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (BIP_upper_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_upper.x[i] = num_PR_2_BIP_upper.x[i] + 1
                        syn_PR_upper_2_BIP_upper[j][i] = new GradSyn_inhibit()
                        BIP_upper[i].soma syn_PR_upper_2_BIP_upper[j][i].loc(0.05)
                        setpointer syn_PR_upper_2_BIP_upper[j][i].V_pre, PR_upper[j].soma.v(0.95)
                        syn_PR_upper_2_BIP_upper[j][i].e = 0
                        syn_PR_upper_2_BIP_upper[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_upper_2_BIP_upper[j][i].g_max = OFFBipSynapticConductance
                    }
                }
            }
            for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_lower_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_upper_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (BIP_upper_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_upper.x[i] = num_PR_2_BIP_upper.x[i] + 1
                        syn_PR_lower_2_BIP_upper[j][i] = new GradSyn_inhibit()
                        BIP_upper[i].soma syn_PR_lower_2_BIP_upper[j][i].loc(0.05)
                        setpointer syn_PR_lower_2_BIP_upper[j][i].V_pre, PR_lower[j].soma.v(0.95)
                        syn_PR_lower_2_BIP_upper[j][i].e = 0
                        syn_PR_lower_2_BIP_upper[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_lower_2_BIP_upper[j][i].g_max = OFFBipSynapticConductance
                    }
                }
            }
        }
        // ----------------------------------Cone_PR to OFF_Bip Synapses end-----------------------------
    }

    // Generate synapses for the lower half of the bipolar cell array
    for i = 0, BIP_total_lower_cells - 1 {
        num_PR_2_BIP_lower.x[i] = 0
        // ----------------------------------Rod_PR to Rod_Bip Synapses Start-----------------------------
        // Check if BC is a Rod Bipolar (type = 1)
        if(BIP_lower_cell_type.x[i][0] == 1) {
            for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Rod (type = 1)
                if(PR_upper_cell_type.x[j][0] == 1) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_lower_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (BIP_lower_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_lower.x[i] = num_PR_2_BIP_lower.x[i] + 1
                        syn_PR_upper_2_BIP_lower[j][i] = new GradSyn_excite()
                        BIP_lower[i].soma syn_PR_upper_2_BIP_lower[j][i].loc(0.05)
                        setpointer syn_PR_upper_2_BIP_lower[j][i].V_pre, PR_upper[j].soma.v(0.95)
                        syn_PR_upper_2_BIP_lower[j][i].e = 0
                        syn_PR_upper_2_BIP_lower[j][i].V_thr = RodBipSynapticThreshold
                        syn_PR_upper_2_BIP_lower[j][i].g_max = RodBipSynapticConductance
                    }
                }
            }
            for j = 0, PR_total_lower_cells - 1 {
                if(PR_lower_cell_type.x[j][0] == 1) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_lower_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (BIP_lower_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_lower.x[i] = num_PR_2_BIP_lower.x[i] + 1
                        syn_PR_lower_2_BIP_lower[j][i] = new GradSyn_excite()
                        BIP_lower[i].soma syn_PR_lower_2_BIP_lower[j][i].loc(0.05)
                        setpointer syn_PR_lower_2_BIP_lower[j][i].V_pre, PR_lower[j].soma.v(0.95)
                        syn_PR_lower_2_BIP_lower[j][i].e = 0
                        syn_PR_lower_2_BIP_lower[j][i].V_thr = RodBipSynapticThreshold
                        syn_PR_lower_2_BIP_lower[j][i].g_max = RodBipSynapticConductance
                    }
                }
            }
        }
        // ----------------------------------Rod_PR to Rod_Bip Synapses End-----------------------------
    
        // ----------------------------------Cone_PR to ON_Bip Synapses Start-----------------------------
        // Check if BC is a ON Bipolar (type = 2)
        if(BIP_lower_cell_type.x[i][0] == 2) {
            for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_upper_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_lower_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (BIP_lower_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_lower.x[i] = num_PR_2_BIP_lower.x[i] + 1
                        syn_PR_upper_2_BIP_lower[j][i] = new GradSyn_excite()
                        BIP_lower[i].soma syn_PR_upper_2_BIP_lower[j][i].loc(0.05)
                        setpointer syn_PR_upper_2_BIP_lower[j][i].V_pre, PR_upper[j].soma.v(0.95)
                        syn_PR_upper_2_BIP_lower[j][i].e = 0
                        syn_PR_upper_2_BIP_lower[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_upper_2_BIP_lower[j][i].g_max = ONBipSynapticConductance
                    }
                }
            }
            for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_lower_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_lower_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (BIP_lower_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_lower.x[i] = num_PR_2_BIP_lower.x[i] + 1
                        syn_PR_lower_2_BIP_lower[j][i] = new GradSyn_excite()
                        BIP_lower[i].soma syn_PR_lower_2_BIP_lower[j][i].loc(0.05)
                        setpointer syn_PR_lower_2_BIP_lower[j][i].V_pre, PR_lower[j].soma.v(0.95)
                        syn_PR_lower_2_BIP_lower[j][i].e = 0
                        syn_PR_lower_2_BIP_lower[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_lower_2_BIP_lower[j][i].g_max = ONBipSynapticConductance
                    }
                }
            }
        }
        // ----------------------------------Cone_PR to ON_Bip Synapses end-----------------------------

        // ----------------------------------Cone_PR to OFF_Bip Synapses Start-----------------------------
        // Check if BC is a OFF Bipolar (type = 3)
        if(BIP_lower_cell_type.x[i][0] == 3) {
            for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_upper_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_lower_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (BIP_lower_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_lower.x[i] = num_PR_2_BIP_lower.x[i] + 1
                        syn_PR_upper_2_BIP_lower[j][i] = new GradSyn_inhibit()
                        BIP_lower[i].soma syn_PR_upper_2_BIP_lower[j][i].loc(0.05)
                        setpointer syn_PR_upper_2_BIP_lower[j][i].V_pre, PR_upper[j].soma.v(0.95)
                        syn_PR_upper_2_BIP_lower[j][i].e = 0
                        syn_PR_upper_2_BIP_lower[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_upper_2_BIP_lower[j][i].g_max = OFFBipSynapticConductance
                    }
                }
            }
            for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_lower_cell_type.x[j][0] == 2) {
                    // Check if distance between 2 cells is < 1x the cell spacing
                    Distance_between_cells = sqrt((BIP_lower_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (BIP_lower_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= BIP_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_BIP_lower.x[i] = num_PR_2_BIP_lower.x[i] + 1
                        syn_PR_lower_2_BIP_lower[j][i] = new GradSyn_inhibit()
                        BIP_lower[i].soma syn_PR_lower_2_BIP_lower[j][i].loc(0.05)
                        setpointer syn_PR_lower_2_BIP_lower[j][i].V_pre, PR_lower[j].soma.v(0.95)
                        syn_PR_lower_2_BIP_lower[j][i].e = 0
                        syn_PR_lower_2_BIP_lower[j][i].V_thr = ConeBipSynapticThreshold
                        syn_PR_lower_2_BIP_lower[j][i].g_max = OFFBipSynapticConductance
                    }
                }
            }
        }
        // ----------------------------------Cone_PR to OFF_Bip Synapses end-----------------------------
    }
}

proc generate_BC2RGC_Synapse() {

    // ---------------------------------------------------------------------------------
    // ----------------------------------GANGLION CELL SYNAPSES-------------------------
    // ---------------------------------------------------------------------------------
    
    num_BIP_2_RGC = new Vector(num_gang)
    num_BIPSyn_2_RGC = new Vector(num_gang)
    total_RGC_syn = new Vector(num_gang)
    random_connec = new Random(RGC_dend_ratio)

    // Generate synapses for RGCs
    for i = 0, num_gang - 1 {
        num_BIP_2_RGC.x[i] = 0
        num_BIPSyn_2_RGC.x[i] = 0
        total_RGC_syn.x[i] = 0
        // Check if RGC is ON Type (type = 1)
        if(ONOFF_type.x[i][0] == 1) {
            // Calculate how many BC connected to RGC
            for j = 0, BIP_total_upper_cells - 1 {
                if(BIP_upper_cell_type.x[j][0] == 2) {
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_upper_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        num_BIP_2_RGC.x[i] = num_BIP_2_RGC.x[i] + 1
                    }
                }
            }
            for j = 0, BIP_total_lower_cells - 1 {
                if(BIP_lower_cell_type.x[j][0] == 2) {
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_lower_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        num_BIP_2_RGC.x[i] = num_BIP_2_RGC.x[i] + 1
                    }
                }
            }
            // Generate synapses for upper half of bipolar cells
            for j = 0, BIP_total_upper_cells - 1 {
                // Check if BIP is ON Type (type = 2)
                if(BIP_upper_cell_type.x[j][0] == 2) {
                    // Check distance between BIP and RGC soma is within RGC dendritic field
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_upper_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        // Check distance between each RGC dendrite and BIP is within BIP dendritic field
                        num_connections = 0
                        for k = 0, num_dends.x[i] - 1 {
                            Distance_between_dend = sqrt((Dend_centres_x[i].x[k] - BIP_upper_positions.x[j][0])^2 + (Dend_centres_y[i].x[k] - BIP_upper_positions.x[j][1])^2)
                            if (Dend_centres_z[i].x[k] > Dend_z.x[i][0]) {
                                if(Distance_between_dend <= BIP_upper_Dendritic_Field_Radius.x[j]) {
                                    d = random_connec.uniform(0, RGC_dend_ratio)
                                    /* if(d < 1) { */
                                        syn_BIP_upper_2_ONRGC[j][i][num_connections] = new GradSyn_bip_gan_excite()
                                        Ganglion[i].dend[k] syn_BIP_upper_2_ONRGC[j][i][num_connections].loc(0.5)
                                        setpointer syn_BIP_upper_2_ONRGC[j][i][num_connections].V_pre, BIP_upper[j].soma.v(0.95)
                                        num_BIPSyn_2_RGC.x[i] = num_BIPSyn_2_RGC.x[i] + 1
                                        num_connections = num_connections + 1
                                    /* } */
                                }
                            }
                        }
                        for l = 0, num_connections - 1 {
                            // Set conductance value to be max_number/num_connections
                            syn_BIP_upper_2_ONRGC[j][i][l].e = 0
                            syn_BIP_upper_2_ONRGC[j][i][l].V_thr = ConeONGanSynapticThreshold
                            syn_BIP_upper_2_ONRGC[j][i][l].g_max = GanSynapticConductance/(num_connections*num_BIP_2_RGC.x[i])
                            syn_BIP_upper_2_ONRGC[j][i][l].V_slope = 10
                            total_RGC_syn.x[i] = total_RGC_syn.x[i] + syn_BIP_upper_2_ONRGC[j][i][l].g_max
                        }
                    }
                }
            }
            // Generate synapses for lower half of bipolar cells
            for j = 0, BIP_total_lower_cells - 1 {
                // Check if BIP is ON Type (type = 2)
                if(BIP_lower_cell_type.x[j][0] == 2) {
                    // Check distance between BIP and RGC soma is within RGC dendritic field
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_lower_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        // Check distance between each RGC dendrite and BIP is within BIP dendritic field
                        num_connections = 0
                        for k = 0, num_dends.x[i] - 1 {
                            Distance_between_dend = sqrt((Dend_centres_x[i].x[k] - BIP_lower_positions.x[j][0])^2 + (Dend_centres_y[i].x[k] - BIP_lower_positions.x[j][1])^2)
                            if (Dend_centres_z[i].x[k] > Dend_z.x[i][0]) {
                                if(Distance_between_dend <= BIP_lower_Dendritic_Field_Radius.x[j]) {
                                    /* d = random_connec.uniform(0, RGC_dend_ratio)
                                    if(d < 1) { */
                                        syn_BIP_lower_2_ONRGC[j][i][num_connections] = new GradSyn_bip_gan_excite()
                                        Ganglion[i].dend[k] syn_BIP_lower_2_ONRGC[j][i][num_connections].loc(0.5)
                                        setpointer syn_BIP_lower_2_ONRGC[j][i][num_connections].V_pre, BIP_lower[j].soma.v(0.95)
                                        num_connections = num_connections + 1
                                        num_BIPSyn_2_RGC.x[i] = num_BIPSyn_2_RGC.x[i] + 1
                                    /* } */
                                }
                            }
                        }
                        for l = 0, num_connections - 1 {
                            // Set conductance value to be max_number/num_connections
                            syn_BIP_lower_2_ONRGC[j][i][l].e = 0
                            syn_BIP_lower_2_ONRGC[j][i][l].V_thr = ConeONGanSynapticThreshold
                            syn_BIP_lower_2_ONRGC[j][i][l].g_max = GanSynapticConductance/(num_connections*num_BIP_2_RGC.x[i])
                            syn_BIP_lower_2_ONRGC[j][i][l].V_slope = 10
                            total_RGC_syn.x[i] = total_RGC_syn.x[i] + syn_BIP_lower_2_ONRGC[j][i][l].g_max
                        }
                    }
                }
            }
        }
        // Check if RGC is OFF Type (type = 2)
        if(ONOFF_type.x[i][0] == 2) {
            // Calculate how many BC connected to RGC
            for j = 0, BIP_total_upper_cells - 1 {
                if(BIP_upper_cell_type.x[j][0] == 3) {
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_upper_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        num_BIP_2_RGC.x[i] = num_BIP_2_RGC.x[i] + 1
                    }
                }
            }
            for j = 0, BIP_total_lower_cells - 1 {
                if(BIP_lower_cell_type.x[j][0] == 3) {
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_lower_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        num_BIP_2_RGC.x[i] = num_BIP_2_RGC.x[i] + 1
                    }
                }
            }
            // Generate synapses for upper half of bipolar cells
            for j = 0, BIP_total_upper_cells - 1 {
                // Check if BIP is OFF Type (type = 3)
                if(BIP_upper_cell_type.x[j][0] == 3) {
                    // Check distance between BIP and RGC soma is within dendritic field
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_upper_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        // Check distance between each RGC dendrite and BIP is within BIP dendritic field
                        num_connections = 0
                        for k = 0, num_dends.x[i] - 1 {
                            Distance_between_dend = sqrt((Dend_centres_x[i].x[k] - BIP_upper_positions.x[j][0])^2 + (Dend_centres_y[i].x[k] - BIP_upper_positions.x[j][1])^2)
                            //if (Dend_centres_z[i].x[k] > Dend_z.x[i][0]) {
                                if(Distance_between_dend <= BIP_upper_Dendritic_Field_Radius.x[j]) {
                                    /* d = random_connec.uniform(0, RGC_dend_ratio)
                                    if(d < 1) { */
                                        syn_BIP_upper_2_OFFRGC[j][i][num_connections] = new GradSyn_bip_gan_inhibit()
                                        Ganglion[i].dend[k] syn_BIP_upper_2_OFFRGC[j][i][num_connections].loc(0.5)
                                        setpointer syn_BIP_upper_2_OFFRGC[j][i][num_connections].V_pre, BIP_upper[j].soma.v(0.95)
                                        num_BIPSyn_2_RGC.x[i] = num_BIPSyn_2_RGC.x[i] + 1
                                        num_connections = num_connections + 1
                                    /* } */
                                }
                            //}
                        }
                        for l = 0, num_connections - 1 {
                            // Set conductance value to be max_number/num_connections
                            syn_BIP_upper_2_OFFRGC[j][i][l].e = 0
                            syn_BIP_upper_2_OFFRGC[j][i][l].V_thr = ConeOFFGanSynapticThreshold
                            syn_BIP_upper_2_OFFRGC[j][i][l].g_max = GanSynapticConductance/(num_connections*num_BIP_2_RGC.x[i])
                            syn_BIP_upper_2_OFFRGC[j][i][l].V_slope = 10
                            total_RGC_syn.x[i] = total_RGC_syn.x[i] + syn_BIP_upper_2_OFFRGC[j][i][l].g_max
                        }
                    }
                }
            }
            // Generate synapses for lower half of bipolar cells
            for j = 0, BIP_total_lower_cells - 1 {
                // Check if BIP is OFF Type (type = 3)
                if(BIP_lower_cell_type.x[j][0] == 3) {
                    // Check distance between BIP and RGC soma is within dendritic field
                    Distance_between_cells = sqrt((Dend_x.x[i][0] - BIP_lower_positions.x[j][0])^2 + (Dend_y.x[i][0] - BIP_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= (RGC_dend_radius.x[i][0] + RGC_dend_buffer)) {
                        // Check distance between each RGC dendrite and BIP is within BIP dendritic field
                        num_connections = 0
                        for k = 0, num_dends.x[i] - 1 {
                            Distance_between_dend = sqrt((Dend_centres_x[i].x[k] - BIP_lower_positions.x[j][0])^2 + (Dend_centres_y[i].x[k] - BIP_lower_positions.x[j][1])^2)
                            //if (Dend_centres_z[i].x[k] > Dend_z.x[i][0]) {
                                if(Distance_between_dend <= BIP_lower_Dendritic_Field_Radius.x[j]) {
                                    /* d = random_connec.uniform(0, RGC_dend_ratio)
                                    if(d < 1) { */
                                        syn_BIP_lower_2_OFFRGC[j][i][num_connections] = new GradSyn_bip_gan_inhibit()
                                        Ganglion[i].dend[k] syn_BIP_lower_2_OFFRGC[j][i][num_connections].loc(0.5)
                                        setpointer syn_BIP_lower_2_OFFRGC[j][i][num_connections].V_pre, BIP_lower[j].soma.v(0.95)
                                        num_BIPSyn_2_RGC.x[i] = num_BIPSyn_2_RGC.x[i] + 1
                                        num_connections = num_connections + 1
                                    /* } */
                                }
                            //}
                        }
                        for l = 0, num_connections - 1 {
                            // Set conductance value to be max_number/num_connections
                            syn_BIP_lower_2_OFFRGC[j][i][l].e = 0
                            syn_BIP_lower_2_OFFRGC[j][i][l].V_thr = ConeOFFGanSynapticThreshold
                            syn_BIP_lower_2_OFFRGC[j][i][l].g_max = GanSynapticConductance/(num_connections*num_BIP_2_RGC.x[i])
                            syn_BIP_lower_2_OFFRGC[j][i][l].V_slope = 10
                            total_RGC_syn.x[i] = total_RGC_syn.x[i] + syn_BIP_lower_2_OFFRGC[j][i][l].g_max
                        }
                    }
                }
            }
        }
    }
    
}

proc generate_HZ_Synapses() {

    // -----------------------------------------------------------------------------------
    // ----------------------------------HORIZONTAL CELL SYNAPSES-------------------------
    // -----------------------------------------------------------------------------------
    
    num_PR_2_HZ_upper = new Vector(HZ_total_upper_cells)
    num_PR_2_HZ_lower = new Vector(HZ_total_lower_cells)

    // Generate synapses for the upper half of the horizontal cell array
    for i = 0, HZ_total_upper_cells - 1 {
        num_PR_2_HZ_upper.x[i] = 0
        for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_upper_cell_type.x[j][0] == 2) {
                    Distance_between_cells = sqrt((HZ_upper_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (HZ_upper_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= HZ_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_HZ_upper.x[i] = num_PR_2_HZ_upper.x[i] + 1
                        // GENERATE SYNAPSE BETWEEN Cone AND HZ
                        pc1 = new ParallelContext()
                        HZ_upper[i].soma syn_PR_upper_2_HZ_upper[j][i] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        PR_upper[j].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        HZ_upper[i].soma pc1.target_var(syn_PR_upper_2_HZ_upper[j][i], &syn_PR_upper_2_HZ_upper[j][i].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_PR_upper_2_HZ_upper[j][i].v_th = HorizontalEXSynapticThreshold
                        syn_PR_upper_2_HZ_upper[j][i].v_slope = 5
                        syn_PR_upper_2_HZ_upper[j][i].g_max = HorizontalEXSynapticConductance

                        // GENERATE SYNAPSE BETWEEN HZ AND Cone
                        pc1 = new ParallelContext()
                        PR_upper[j].soma syn_HZ_upper_2_PR_upper[i][j] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        HZ_upper[i].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        PR_upper[j].soma pc1.target_var(syn_HZ_upper_2_PR_upper[i][j], &syn_HZ_upper_2_PR_upper[i][j].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_HZ_upper_2_PR_upper[i][j].v_th = HorizontalINSynapticThreshold
                        syn_HZ_upper_2_PR_upper[i][j].v_slope = 5
                        syn_HZ_upper_2_PR_upper[i][j].g_max = HorizontalINSynapticConductance
                    }
                }
        }
        for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_lower_cell_type.x[j][0] == 2) {
                    Distance_between_cells = sqrt((HZ_upper_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (HZ_upper_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= HZ_upper_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_HZ_upper.x[i] = num_PR_2_HZ_upper.x[i] + 1
                        // GENERATE SYNAPSE BETWEEN Cone AND HZ
                        pc1 = new ParallelContext()
                        HZ_upper[i].soma syn_PR_lower_2_HZ_upper[j][i] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        PR_lower[j].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        HZ_upper[i].soma pc1.target_var(syn_PR_lower_2_HZ_upper[j][i], &syn_PR_lower_2_HZ_upper[j][i].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_PR_lower_2_HZ_upper[j][i].v_th = HorizontalEXSynapticThreshold
                        syn_PR_lower_2_HZ_upper[j][i].v_slope = 5
                        syn_PR_lower_2_HZ_upper[j][i].g_max = HorizontalEXSynapticConductance

                        // GENERATE SYNAPSE BETWEEN HZ AND Cone
                        pc1 = new ParallelContext()
                        PR_lower[j].soma syn_HZ_upper_2_PR_lower[i][j] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        HZ_upper[i].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        PR_lower[j].soma pc1.target_var(syn_HZ_upper_2_PR_lower[i][j], &syn_HZ_upper_2_PR_lower[i][j].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_HZ_upper_2_PR_lower[i][j].v_th = HorizontalINSynapticThreshold
                        syn_HZ_upper_2_PR_lower[i][j].v_slope = 5
                        syn_HZ_upper_2_PR_lower[i][j].g_max = HorizontalINSynapticConductance
                    }
                }
        }
    }

    // Generate synapses for the lower half of the horizontal cell array
    for i = 0, HZ_total_lower_cells - 1 {
        num_PR_2_HZ_lower.x[i] = 0
        for j = 0, PR_total_upper_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_upper_cell_type.x[j][0] == 2) {
                    Distance_between_cells = sqrt((HZ_lower_positions.x[i][0] - PR_upper_positions.x[j][0])^2 + (HZ_lower_positions.x[i][1] - PR_upper_positions.x[j][1])^2)
                    if(Distance_between_cells <= HZ_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_HZ_lower.x[i] = num_PR_2_HZ_lower.x[i] + 1
                        // GENERATE SYNAPSE BETWEEN Cone AND HZ
                        pc1 = new ParallelContext()
                        HZ_lower[i].soma syn_PR_upper_2_HZ_lower[j][i] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        PR_upper[j].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        HZ_lower[i].soma pc1.target_var(syn_PR_upper_2_HZ_lower[j][i], &syn_PR_upper_2_HZ_lower[j][i].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_PR_upper_2_HZ_lower[j][i].v_th = HorizontalEXSynapticThreshold
                        syn_PR_upper_2_HZ_lower[j][i].v_slope = 5
                        syn_PR_upper_2_HZ_lower[j][i].g_max = HorizontalEXSynapticConductance

                        // GENERATE SYNAPSE BETWEEN HZ AND Cone
                        pc1 = new ParallelContext()
                        PR_upper[j].soma syn_HZ_lower_2_PR_upper[i][j] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        HZ_lower[i].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        PR_upper[j].soma pc1.target_var(syn_HZ_lower_2_PR_upper[i][j], &syn_HZ_lower_2_PR_upper[i][j].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_HZ_lower_2_PR_upper[i][j].v_th = HorizontalINSynapticThreshold
                        syn_HZ_lower_2_PR_upper[i][j].v_slope = 5
                        syn_HZ_lower_2_PR_upper[i][j].g_max = HorizontalINSynapticConductance
                    }
                }
        }
        for j = 0, PR_total_lower_cells - 1 {
                // Check if PR is a Cone (type = 2)
                if(PR_lower_cell_type.x[j][0] == 2) {
                    Distance_between_cells = sqrt((HZ_lower_positions.x[i][0] - PR_lower_positions.x[j][0])^2 + (HZ_lower_positions.x[i][1] - PR_lower_positions.x[j][1])^2)
                    if(Distance_between_cells <= HZ_lower_Dendritic_Field_Radius.x[i]) {
                        num_PR_2_HZ_lower.x[i] = num_PR_2_HZ_lower.x[i] + 1
                        // GENERATE SYNAPSE BETWEEN Cone AND HZ
                        pc1 = new ParallelContext()
                        HZ_lower[i].soma syn_PR_lower_2_HZ_lower[j][i] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        PR_lower[j].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        HZ_lower[i].soma pc1.target_var(syn_PR_lower_2_HZ_lower[j][i], &syn_PR_lower_2_HZ_lower[j][i].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_PR_lower_2_HZ_lower[j][i].v_th = HorizontalEXSynapticThreshold
                        syn_PR_lower_2_HZ_lower[j][i].v_slope = 5
                        syn_PR_lower_2_HZ_lower[j][i].g_max = HorizontalEXSynapticConductance

                        // GENERATE SYNAPSE BETWEEN HZ AND Cone
                        pc1 = new ParallelContext()
                        PR_lower[j].soma syn_HZ_lower_2_PR_lower[i][j] = new Synapse_HorPR(0.5)
                        synapse_counter = synapse_counter + 1
                        HZ_lower[i].soma pc1.source_var(&v(0.5), SVCounter)
                        SVCounter = SVCounter + 1
                        PR_lower[j].soma pc1.target_var(syn_HZ_lower_2_PR_lower[i][j], &syn_HZ_lower_2_PR_lower[i][j].V_pre, TVCounter)
                        TVCounter = TVCounter + 1
                        {pc1.setup_transfer() }
                        syn_HZ_lower_2_PR_lower[i][j].v_th = HorizontalINSynapticThreshold
                        syn_HZ_lower_2_PR_lower[i][j].v_slope = 5
                        syn_HZ_lower_2_PR_lower[i][j].g_max = HorizontalINSynapticConductance
                    }
                }
        }
    }

}

proc generate_AII_Synapses() {

    // -------------------------------------------------------------------------------------
    // ----------------------------------AII AMACRINE CELL SYNAPSES-------------------------
    // -------------------------------------------------------------------------------------

    num_RBC_2_AII_upper = new Vector(AII_total_upper_cells)
    num_RBC_2_AII_lower = new Vector(AII_total_lower_cells)
    num_ONBC_2_AII_upper = new Vector(AII_total_upper_cells)
    num_ONBC_2_AII_lower = new Vector(AII_total_lower_cells)
    num_AII_2_OFFBC_upper = new Vector(AII_total_upper_cells)
    num_AII_2_OFFBC_lower = new Vector(AII_total_lower_cells)
    num_AII_2_OFF_RGC_upper = new Vector(AII_total_upper_cells)
    num_AII_2_OFF_RGC_lower = new Vector(AII_total_lower_cells)

    // Generate synapses for the upper half of the AII amacrine cell array
    for i = 0, AII_total_upper_cells - 1 {
        num_RBC_2_AII_upper.x[i] = 0
        num_ONBC_2_AII_upper.x[i] = 0
        num_AII_2_OFFBC_upper.x[i] = 0
        num_AII_2_OFF_RGC_upper.x[i] = 0
        // ----------------------------------Rod_Bip to AII Synapses Start-----------------------------
        for j = 0, BIP_total_upper_cells - 1 {
            Distance_between_cells = sqrt((AII_upper_positions.x[i][0] - BIP_upper_positions.x[j][0])^2 + (AII_upper_positions.x[i][1] - BIP_upper_positions.x[j][1])^2)
            // Check if BC is a Rod Bipolar (type = 1)
            if(BIP_upper_cell_type.x[j][0] == 1) {
                if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                    num_RBC_2_AII_upper.x[i] = num_RBC_2_AII_upper.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN ROD_BIP AND AII AC
                    syn_AII_upper_2_BIP_upper[i][j] = new GradSyn_bip_ama_excite()
                    AII_upper[i].soma syn_AII_upper_2_BIP_upper[i][j].loc(0.05)
                    setpointer syn_AII_upper_2_BIP_upper[i][j].V_pre, BIP_upper[j].soma.v(0.95)
                    syn_AII_upper_2_BIP_upper[i][j].e = 0
                    syn_AII_upper_2_BIP_upper[i][j].V_thr = RodBip2AmaSynapticThreshold
                    syn_AII_upper_2_BIP_upper[i][j].g_max = RodBip2AmaSynapticConductance
                }
            }
            // Check if BC is a ON Bipolar (type = 2)
            if(BIP_upper_cell_type.x[j][0] == 2) {
                if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                    num_ONBC_2_AII_upper.x[i] = num_ONBC_2_AII_upper.x[i] + 1
                    // GENERATE GAP JUNCTION BETWEEN ON_BIP AND AII AC
                    AmaGapJunctions[NumAmaGapConnections] = new AII_ONBCGap()
                    AII_upper[i].soma AmaGapJunctions[NumAmaGapConnections].src(0.05)
                    BIP_upper[j].soma AmaGapJunctions[NumAmaGapConnections].target(0.95)
                    AmaGapJunctions[NumAmaGapConnections].g(Ama2BipGap)
                    NumAmaGapConnections = NumAmaGapConnections + 1
                }
            }
            // Check if BC is a OFF Bipolar (type = 3)
            if(BIP_upper_cell_type.x[j][0] == 3) {
                if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                    num_AII_2_OFFBC_upper.x[i] = num_AII_2_OFFBC_upper.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN AII AC AND OFF_BIP
                    syn_AII_upper_2_BIP_upper[i][j] = new GradSyn_ama_bip_inhibit()
                    BIP_upper[j].soma syn_AII_upper_2_BIP_upper[i][j].loc(0.05)
                    setpointer syn_AII_upper_2_BIP_upper[i][j].V_pre, AII_upper[i].soma.v(0.95)
                    syn_AII_upper_2_BIP_upper[i][j].e = 0 
                    syn_AII_upper_2_BIP_upper[i][j].V_thr = Ama2ConeOFFBipThreshold
                    syn_AII_upper_2_BIP_upper[i][j].g_max = -AmaSynapticConductance
                }
            }
        }
        for j = 0, BIP_total_lower_cells - 1 {
            Distance_between_cells = sqrt((AII_upper_positions.x[i][0] - BIP_lower_positions.x[j][0])^2 + (AII_upper_positions.x[i][1] - BIP_lower_positions.x[j][1])^2)
            // Check if BC is a Rod Bipolar (type = 1)
            if(BIP_lower_cell_type.x[j][0] == 1) {
                if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                    num_RBC_2_AII_upper.x[i] = num_RBC_2_AII_upper.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN ROD_BIP AND AII AC
                    syn_AII_upper_2_BIP_lower[i][j] = new GradSyn_bip_ama_excite()
                    AII_upper[i].soma syn_AII_upper_2_BIP_lower[i][j].loc(0.05)
                    setpointer syn_AII_upper_2_BIP_lower[i][j].V_pre, BIP_lower[j].soma.v(0.95)
                    syn_AII_upper_2_BIP_lower[i][j].e = 0
                    syn_AII_upper_2_BIP_lower[i][j].V_thr = RodBip2AmaSynapticThreshold
                    syn_AII_upper_2_BIP_lower[i][j].g_max = RodBip2AmaSynapticConductance
                }
            }
            // Check if BC is a ON Bipolar (type = 2)
            if(BIP_lower_cell_type.x[j][0] == 2) {
                if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                    num_ONBC_2_AII_upper.x[i] = num_ONBC_2_AII_upper.x[i] + 1
                    // GENERATE GAP JUNCTION BETWEEN ON_BIP AND AII AC
                    AmaGapJunctions[NumAmaGapConnections] = new AII_ONBCGap()
                    AII_upper[i].soma AmaGapJunctions[NumAmaGapConnections].src(0.05)
                    BIP_lower[j].soma AmaGapJunctions[NumAmaGapConnections].target(0.95)
                    AmaGapJunctions[NumAmaGapConnections].g(Ama2BipGap)
                    NumAmaGapConnections = NumAmaGapConnections + 1
                }
            }
            // Check if BC is a OFF Bipolar (type = 3)
            if(BIP_lower_cell_type.x[j][0] == 3) {
                if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                    num_AII_2_OFFBC_upper.x[i] = num_AII_2_OFFBC_upper.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN AII AC AND OFF_BIP
                    syn_AII_upper_2_BIP_lower[i][j] = new GradSyn_ama_bip_inhibit()
                    BIP_lower[j].soma syn_AII_upper_2_BIP_lower[i][j].loc(0.05)
                    setpointer syn_AII_upper_2_BIP_lower[i][j].V_pre, AII_upper[i].soma.v(0.95)
                    syn_AII_upper_2_BIP_lower[i][j].e = 0 
                    syn_AII_upper_2_BIP_lower[i][j].V_thr = Ama2ConeOFFBipThreshold
                    syn_AII_upper_2_BIP_lower[i][j].g_max = -AmaSynapticConductance
                }
            }
        }
        // ----------------------------------Rod_Bip to AII Synapses End-----------------------------

        // ----------------------------------AII to OFF_Gan Synapses Start-----------------------------
        for j = 0, num_gang - 1 {
            // Check if RGC is OFF Type (type = 2)
            if(ONOFF_type.x[j][0] == 2) {
                if(num_BIP_2_RGC.x[j] > 0) {
                    Distance_between_cells = sqrt((AII_upper_positions.x[i][0] - Dend_x.x[j][0])^2 + (AII_upper_positions.x[i][1] - Dend_y.x[j][0])^2)
                    if(Distance_between_cells < AII_upper_Dendritic_Field_Radius.x[i]) {
                        num_AII_2_OFF_RGC_upper.x[i] = num_AII_2_OFF_RGC_upper.x[i] + 1
                        num_connections = 0
                        for k = 0, num_dends.x[j] - 1 {
                            //if (Dend_centres_z[j].x[k] > Dend_z.x[j][0]) {
                                d = random_connec.uniform(0, RGC_dend_ratio)
                                    if(d < 1) {
                                        // GENERATE SYNAPSE BETWEEN AII AC AND OFF_GAN
                                        syn_AII_upper_2_RGC[i][j][num_connections] = new GradSyn_ama_gan_inhibit()
                                        Ganglion[j].dend[k] syn_AII_upper_2_RGC[i][j][num_connections].loc(0.05)
                                        setpointer syn_AII_upper_2_RGC[i][j][num_connections].V_pre, AII_upper[i].soma.v(0.95)
                                        syn_AII_upper_2_RGC[i][j][num_connections].e = 0
                                        syn_AII_upper_2_RGC[i][j][num_connections].V_thr = AmaSynapticThreshold
                                        syn_AII_upper_2_RGC[i][j][num_connections].g_max = -Ama2GanSynapticConductance/4
                                        num_connections = num_connections + 1
                                    }
                            //}
                        }
                    }
                }
            }
        }
        // ----------------------------------AII to OFF_Gan Synapses End-----------------------------
    }

    // Generate synapses for the lower half of the AII amacrine cell array
    for i = 0, AII_total_lower_cells - 1 {
        num_RBC_2_AII_lower.x[i] = 0
        num_ONBC_2_AII_lower.x[i] = 0
        num_AII_2_OFFBC_lower.x[i] = 0
        num_AII_2_OFF_RGC_lower.x[i] = 0
        // ----------------------------------Rod_Bip to AII Synapses Start-----------------------------
        for j = 0, BIP_total_upper_cells - 1 {
            Distance_between_cells = sqrt((AII_lower_positions.x[i][0] - BIP_upper_positions.x[j][0])^2 + (AII_lower_positions.x[i][1] - BIP_upper_positions.x[j][1])^2)
            // Check if BC is a Rod Bipolar (type = 1)
            if(BIP_upper_cell_type.x[j][0] == 1) {
                if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                    num_RBC_2_AII_lower.x[i] = num_RBC_2_AII_lower.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN ROD_BIP AND AII AC
                    syn_AII_lower_2_BIP_upper[i][j] = new GradSyn_bip_ama_excite()
                    AII_lower[i].soma syn_AII_lower_2_BIP_upper[i][j].loc(0.05)
                    setpointer syn_AII_lower_2_BIP_upper[i][j].V_pre, BIP_upper[j].soma.v(0.95)
                    syn_AII_lower_2_BIP_upper[i][j].e = 0
                    syn_AII_lower_2_BIP_upper[i][j].V_thr = RodBip2AmaSynapticThreshold
                    syn_AII_lower_2_BIP_upper[i][j].g_max = RodBip2AmaSynapticConductance
                }
            }
            // Check if BC is a ON Bipolar (type = 2)
            if(BIP_upper_cell_type.x[j][0] == 2) {
                if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                    num_ONBC_2_AII_lower.x[i] = num_ONBC_2_AII_lower.x[i] + 1
                    // GENERATE GAP JUNCTION BETWEEN ON_BIP AND AII AC
                    AmaGapJunctions[NumAmaGapConnections] = new AII_ONBCGap()
                    AII_lower[i].soma AmaGapJunctions[NumAmaGapConnections].src(0.05)
                    BIP_upper[j].soma AmaGapJunctions[NumAmaGapConnections].target(0.95)
                    AmaGapJunctions[NumAmaGapConnections].g(Ama2BipGap)
                    NumAmaGapConnections = NumAmaGapConnections + 1
                }
            }
            // Check if BC is a OFF Bipolar (type = 3)
            if(BIP_upper_cell_type.x[j][0] == 3) {
                if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                    num_AII_2_OFFBC_lower.x[i] = num_AII_2_OFFBC_lower.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN AII AC AND OFF_BIP
                    syn_AII_lower_2_BIP_upper[i][j] = new GradSyn_ama_bip_inhibit()
                    BIP_upper[j].soma syn_AII_lower_2_BIP_upper[i][j].loc(0.05)
                    setpointer syn_AII_lower_2_BIP_upper[i][j].V_pre, AII_lower[i].soma.v(0.95)
                    syn_AII_lower_2_BIP_upper[i][j].e = 0
                    syn_AII_lower_2_BIP_upper[i][j].V_thr = Ama2ConeOFFBipThreshold
                    syn_AII_lower_2_BIP_upper[i][j].g_max = -AmaSynapticConductance 
                }
            }
        }
        for j = 0, BIP_total_lower_cells - 1 {
            Distance_between_cells = sqrt((AII_lower_positions.x[i][0] - BIP_lower_positions.x[j][0])^2 + (AII_lower_positions.x[i][1] - BIP_lower_positions.x[j][1])^2)
            // Check if BC is a Rod Bipolar (type = 1)
            if(BIP_lower_cell_type.x[j][0] == 1) {
                if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                    num_RBC_2_AII_lower.x[i] = num_RBC_2_AII_lower.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN ROD_BIP AND AII AC
                    syn_AII_lower_2_BIP_lower[i][j] = new GradSyn_bip_ama_excite()
                    AII_lower[i].soma syn_AII_lower_2_BIP_lower[i][j].loc(0.05)
                    setpointer syn_AII_lower_2_BIP_lower[i][j].V_pre, BIP_lower[j].soma.v(0.95)
                    syn_AII_lower_2_BIP_lower[i][j].e = 0
                    syn_AII_lower_2_BIP_lower[i][j].V_thr = RodBip2AmaSynapticThreshold
                    syn_AII_lower_2_BIP_lower[i][j].g_max = RodBip2AmaSynapticConductance
                }
            }
            // Check if BC is a ON Bipolar (type = 2)
            if(BIP_lower_cell_type.x[j][0] == 2) {
                if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                    num_ONBC_2_AII_lower.x[i] = num_ONBC_2_AII_lower.x[i] + 1
                    // GENERATE GAP JUNCTION BETWEEN ON_BIP AND AII AC
                    AmaGapJunctions[NumAmaGapConnections] = new AII_ONBCGap()
                    AII_lower[i].soma AmaGapJunctions[NumAmaGapConnections].src(0.05)
                    BIP_lower[j].soma AmaGapJunctions[NumAmaGapConnections].target(0.95)
                    AmaGapJunctions[NumAmaGapConnections].g(Ama2BipGap)
                    NumAmaGapConnections = NumAmaGapConnections + 1
                }
            }
            // Check if BC is a OFF Bipolar (type = 3)
            if(BIP_lower_cell_type.x[j][0] == 3) {
                if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                    num_AII_2_OFFBC_lower.x[i] = num_AII_2_OFFBC_lower.x[i] + 1
                    // GENERATE SYNAPSE BETWEEN AII AC AND OFF_BIP
                    syn_AII_lower_2_BIP_lower[i][j] = new GradSyn_ama_bip_inhibit()
                    BIP_lower[j].soma syn_AII_lower_2_BIP_lower[i][j].loc(0.05)
                    setpointer syn_AII_lower_2_BIP_lower[i][j].V_pre, AII_lower[i].soma.v(0.95)
                    syn_AII_lower_2_BIP_lower[i][j].e = 0
                    syn_AII_lower_2_BIP_lower[i][j].V_thr = Ama2ConeOFFBipThreshold
                    syn_AII_lower_2_BIP_lower[i][j].g_max = -AmaSynapticConductance 
                }
            }
        }
        // ----------------------------------Rod_Bip to AII Synapses End-----------------------------

        // ----------------------------------AII to OFF_Gan Synapses Start-----------------------------
        for j = 0, num_gang - 1 {
            // Check if RGC is OFF Type (type = 2)
            if(ONOFF_type.x[j][0] == 2) {
                if(num_BIP_2_RGC.x[j] > 0) {
                    Distance_between_cells = sqrt((AII_lower_positions.x[i][0] - Dend_x.x[j][0])^2 + (AII_lower_positions.x[i][1] - Dend_y.x[j][0])^2)
                    if(Distance_between_cells < AII_lower_Dendritic_Field_Radius.x[i]) {
                        num_AII_2_OFF_RGC_lower.x[i] = num_AII_2_OFF_RGC_lower.x[i] + 1
                        num_connections = 0
                        for k = 0, num_dends.x[j] - 1 {
                            //if (Dend_centres_z[j].x[k] > Dend_z.x[j][0]) {
                                d = random_connec.uniform(0, RGC_dend_ratio)
                                    if(d < 1) {
                                        // GENERATE SYNAPSE BETWEEN AII AC AND OFF_GAN
                                        syn_AII_lower_2_RGC[i][j][num_connections] = new GradSyn_ama_gan_inhibit()
                                        Ganglion[j].dend[k] syn_AII_lower_2_RGC[i][j][num_connections].loc(0.05)
                                        setpointer syn_AII_lower_2_RGC[i][j][num_connections].V_pre, AII_lower[i].soma.v(0.95)
                                        syn_AII_lower_2_RGC[i][j][num_connections].e = 0
                                        syn_AII_lower_2_RGC[i][j][num_connections].V_thr = AmaSynapticThreshold
                                        syn_AII_lower_2_RGC[i][j][num_connections].g_max = -Ama2GanSynapticConductance/4
                                        num_connections = num_connections + 1
                                    }
                            //}
                        }
                    }
                }
            }
        }
        // ----------------------------------AII to OFF_Gan Synapses End-----------------------------
    }

}

// -------------------------------------------------------------------------------------
// ----------------------------------Create Clamps to Initialise------------------------
// -------------------------------------------------------------------------------------

proc create_Clamps() {

    for i = 0, PR_total_upper_cells - 1 {
        PR_upper[i].soma {
            vc_PR_upper[i] = new VClamp(0.5)
            vc_PR_upper[i].dur[0] = 200
            vc_PR_upper[i].amp[0] = -40
        }
    }
    for i = 0, PR_total_lower_cells - 1 {
        PR_lower[i].soma {
            vc_PR_lower[i] = new VClamp(0.5)
            vc_PR_lower[i].dur[0] = 200
            vc_PR_lower[i].amp[0] = -40
        }
    }

    for i = 0, HZ_total_upper_cells - 1 {
        HZ_upper[i].soma {
            vc_HZ_upper[i] = new VClamp(0.5)
            vc_HZ_upper[i].dur[0] = 200
            vc_HZ_upper[i].amp[0] = -20
        }
    }
    for i = 0, HZ_total_lower_cells - 1 {
        HZ_lower[i].soma {
            vc_HZ_lower[i] = new VClamp(0.5)
            vc_HZ_lower[i].dur[0] = 200
            vc_HZ_lower[i].amp[0] = -20
        }
    }

    for i = 0, BIP_total_upper_cells - 1 {
        BIP_upper[i].soma {
            vc_BIP_upper[i] = new VClamp(0.5)
            vc_BIP_upper[i].dur[0] = 200
            vc_BIP_upper[i].amp[0] = -39
        }
    }
    for i = 0, BIP_total_lower_cells - 1 {
        BIP_lower[i].soma {
            vc_BIP_lower[i] = new VClamp(0.5)
            vc_BIP_lower[i].dur[0] = 200
            vc_BIP_lower[i].amp[0] = -39
        }
    }

    for i = 0, AII_total_upper_cells - 1 {
        AII_upper[i].soma {
            vc_AII_upper[i]= new VClamp(0.5)
            vc_AII_upper[i].dur[0] = 200
            vc_AII_upper[i].amp[0] = -65
        }
/*         AII_upper[i].IS {

        }
        AII_upper[i].arm {

        } */
    }

    for i = 0, AII_total_lower_cells - 1 {
        AII_lower[i].soma {
            vc_AII_lower[i]= new VClamp(0.5)
            vc_AII_lower[i].dur[0] = 200
            vc_AII_lower[i].amp[0] = -65
        }
/*         AII_lower[i].IS {

        }
        AII_lower[i].arm {

        } */
    }

}