# Define names of output files
filenamec = 'gatescan_conf.nc'
filenamea = 'gatescan.nc'

# Read the zero bias calculation
device_configuration = nlread('InAs_5nm_pin.nc', DeviceConfiguration)[0]

# Get the calculator
calculator = device_configuration.calculator()

# Set the source-drain voltage of the calculator
calculator=calculator(electrode_voltages=(0*Volt, 0.5*Volt))

# Get the metallic regions
metallic_regions = device_configuration.metallicRegions()
# Define positive bias voltages
gate_voltage_list=numpy.arange(10)*0.1*Volt

#make loop
for gate_voltage in gate_voltage_list:
    # Change the gate voltages
    new_regions = [m(value = gate_voltage) for m in metallic_regions]
    device_configuration.setMetallicRegions(new_regions)

    # Set new calculator with modified electrode voltages on the configuration
    # use the self consistent state of the old calculation as starting input.
    device_configuration.setCalculator(
          calculator(),
          initial_state=device_configuration)
    device_configuration.update()
    nlsave(filenamec, device_configuration)

    electrostatic_difference_potential = ElectrostaticDifferencePotential(device_configuration)
    nlsave(filenamea, electrostatic_difference_potential)

# Redoing everything for negative voltages, to start from zero bias again
# Read the zero bias calculation
device_configuration = nlread('InAs_5nm_pin.nc', DeviceConfiguration)[0]
# Get the calculator
calculator = device_configuration.calculator()
# Set the source-drain voltage of the calculator
calculator=calculator(electrode_voltages=(0*Volt, 0.5*Volt))
# Get the metallic regions
metallic_regions = device_configuration.metallicRegions()
# Define positive bias voltages
gate_voltage_list=numpy.arange(6)*0.1*Volt*(-1.0)

#make loop
for gate_voltage in gate_voltage_list:
    # Change the gate voltages
    new_regions = [m(value = gate_voltage) for m in metallic_regions]
    device_configuration.setMetallicRegions(new_regions)

    # Set new calculator with modified electrode voltages on the configuration
    # use the self consistent state of the old calculation as starting input.
    device_configuration.setCalculator(
          calculator(),
          initial_state=device_configuration)
    device_configuration.update()
    nlsave(filenamec, device_configuration)

    electrostatic_difference_potential = ElectrostaticDifferencePotential(device_configuration)
    nlsave(filenamea, electrostatic_difference_potential)
