# Setup the DFTB Calculator
basis_set = DFTBDirectory("dftb/mio/")
pair_potentials = DFTBDirectory("dftb/mio/")

numerical_accuracy_parameters = NumericalAccuracyParameters(
    k_point_sampling=(1, 1, 51),
    )

iteration_control_parameters = IterationControlParameters()

calculator = SlaterKosterCalculator(
    basis_set=basis_set,
    pair_potentials=pair_potentials,
    numerical_accuracy_parameters=numerical_accuracy_parameters,
    iteration_control_parameters=iteration_control_parameters,
    spin_polarization=True,
    )

#loop over different NanoRibbons
for n in range(1,10):
    #generate the nanoribbon
    bulk_configuration = NanoRibbon(n,n)

    #Determine the initial spin of the configuration
    elements = bulk_configuration.elements()
    coords = bulk_configuration.fractionalCoordinates()
    scaled_spins = numpy.zeros(len(elements))

    #find the index of the two edge Carbon atoms,
    ymin = 0.5
    ymax = 0.5
    imin=0
    imax=0
    for i in range(len(elements)):
        if coords[i][1] < ymin and elements[i] == Carbon:
            ymin = coords[i][1]
            imin = i
        if coords[i][1] > ymax and elements[i] == Carbon:
            ymax = coords[i][1]
            imax = i

    # set opposite spins on the edge Carbon atoms
    scaled_spins[imin] = 1
    scaled_spins[imax] = -1

    #attach calculator, and set the initial spin
    bulk_configuration.setCalculator(
        calculator(),
        initial_spin=InitialSpin(scaled_spins=scaled_spins),
        )
    bulk_configuration.update()

    # Calculate Bandstructure
    bandstructure = Bandstructure(
        configuration=bulk_configuration,
        route=['G', 'Z'],
        points_per_segment=200,
        bands_above_fermi_level=All
    )
    nlsave('bandgap.nc',bandstructure)

