# -*- coding: utf-8 -*-

for run in range(0,10):

    # -------------------------------------------------------------
    # Two-probe Configuration
    # -------------------------------------------------------------

    # -------------------------------------------------------------
    # Left Electrode
    # -------------------------------------------------------------

    # Set up lattice
    vector_a = [8.65127469112, 0.0, 0.0]*Angstrom
    vector_b = [0.0, 8.65127469112, 0.0]*Angstrom
    vector_c = [0.0, 0.0, 4.07825]*Angstrom
    left_electrode_lattice = UnitCell(vector_a, vector_b, vector_c)

    # Define elements
    left_electrode_elements = [Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold]

    # Define coordinates
    left_electrode_coordinates = [[ 1.441879115187,  1.441879115187,  1.0195625     ],
                                  [ 4.325637345561,  1.441879115187,  1.0195625     ],
                                  [ 7.209395575935,  1.441879115187,  1.0195625     ],
                                  [ 1.441879115187,  4.325637345561,  1.0195625     ],
                                  [ 4.325637345561,  4.325637345561,  1.0195625     ],
                                  [ 7.209395575935,  4.325637345561,  1.0195625     ],
                                  [ 1.441879115187,  7.209395575935,  1.0195625     ],
                                  [ 4.325637345561,  7.209395575935,  1.0195625     ],
                                  [ 7.209395575935,  7.209395575935,  1.0195625     ],
                                  [ 0.            ,  0.            ,  3.0586875     ],
                                  [ 2.883758230374,  0.            ,  3.0586875     ],
                                  [ 5.767516460748,  0.            ,  3.0586875     ],
                                  [ 0.            ,  2.883758230374,  3.0586875     ],
                                  [ 2.883758230374,  2.883758230374,  3.0586875     ],
                                  [ 5.767516460748,  2.883758230374,  3.0586875     ],
                                  [ 0.            ,  5.767516460748,  3.0586875     ],
                                  [ 2.883758230374,  5.767516460748,  3.0586875     ],
                                  [ 5.767516460748,  5.767516460748,  3.0586875     ]]*Angstrom

    # Set up configuration
    left_electrode = BulkConfiguration(
        bravais_lattice=left_electrode_lattice,
        elements=left_electrode_elements,
        cartesian_coordinates=left_electrode_coordinates
        )

    # -------------------------------------------------------------
    # Right Electrode
    # -------------------------------------------------------------

    # Set up lattice
    vector_a = [8.65127469112, 0.0, 0.0]*Angstrom
    vector_b = [0.0, 8.65127469112, 0.0]*Angstrom
    vector_c = [0.0, 0.0, 4.07825]*Angstrom
    right_electrode_lattice = UnitCell(vector_a, vector_b, vector_c)

    # Define elements
    right_electrode_elements = [Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                                Gold, Gold, Gold, Gold, Gold, Gold, Gold]

    # Define coordinates
    right_electrode_coordinates = [[ 1.441879115187,  1.441879115187,  1.0195625     ],
                                   [ 4.325637345561,  1.441879115187,  1.0195625     ],
                                   [ 7.209395575935,  1.441879115187,  1.0195625     ],
                                   [ 1.441879115187,  4.325637345561,  1.0195625     ],
                                   [ 4.325637345561,  4.325637345561,  1.0195625     ],
                                   [ 7.209395575935,  4.325637345561,  1.0195625     ],
                                   [ 1.441879115187,  7.209395575935,  1.0195625     ],
                                   [ 4.325637345561,  7.209395575935,  1.0195625     ],
                                   [ 7.209395575935,  7.209395575935,  1.0195625     ],
                                   [ 0.            ,  0.            ,  3.0586875     ],
                                   [ 2.883758230374,  0.            ,  3.0586875     ],
                                   [ 5.767516460748,  0.            ,  3.0586875     ],
                                   [ 0.            ,  2.883758230374,  3.0586875     ],
                                   [ 2.883758230374,  2.883758230374,  3.0586875     ],
                                   [ 5.767516460748,  2.883758230374,  3.0586875     ],
                                   [ 0.            ,  5.767516460748,  3.0586875     ],
                                   [ 2.883758230374,  5.767516460748,  3.0586875     ],
                                   [ 5.767516460748,  5.767516460748,  3.0586875     ]]*Angstrom

    # Set up configuration
    right_electrode = BulkConfiguration(
        bravais_lattice=right_electrode_lattice,
        elements=right_electrode_elements,
        cartesian_coordinates=right_electrode_coordinates
        )

    # -------------------------------------------------------------
    # Central Region
    # -------------------------------------------------------------

    # Set up lattice
    vector_a = [8.65127469112, 0.0, 0.0]*Angstrom
    vector_b = [0.0, 8.65127469112, 0.0]*Angstrom
    vector_c = [0.0, 0.0, 20.39125]*Angstrom
    central_region_lattice = UnitCell(vector_a, vector_b, vector_c)

    # Define elements
    central_region_elements = [Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold, Gold,
                               Gold, Gold]

    # Define coordinates
    central_region_coordinates = [[  1.441879115187,   1.441879115187,   1.0195625     ],
                                  [  4.325637345561,   1.441879115187,   1.0195625     ],
                                  [  7.209395575935,   1.441879115187,   1.0195625     ],
                                  [  1.441879115187,   4.325637345561,   1.0195625     ],
                                  [  4.325637345561,   4.325637345561,   1.0195625     ],
                                  [  7.209395575935,   4.325637345561,   1.0195625     ],
                                  [  1.441879115187,   7.209395575935,   1.0195625     ],
                                  [  4.325637345561,   7.209395575935,   1.0195625     ],
                                  [  7.209395575935,   7.209395575935,   1.0195625     ],
                                  [  0.            ,   0.            ,   3.0586875     ],
                                  [  2.883758230374,   0.            ,   3.0586875     ],
                                  [  5.767516460748,   0.            ,   3.0586875     ],
                                  [  0.            ,   2.883758230374,   3.0586875     ],
                                  [  2.883758230374,   2.883758230374,   3.0586875     ],
                                  [  5.767516460748,   2.883758230374,   3.0586875     ],
                                  [  0.            ,   5.767516460748,   3.0586875     ],
                                  [  2.883758230374,   5.767516460748,   3.0586875     ],
                                  [  5.767516460748,   5.767516460748,   3.0586875     ],
                                  [  1.441879115187,   1.441879115187,   5.0978125     ],
                                  [  4.325637345561,   1.441879115187,   5.0978125     ],
                                  [  7.209395575935,   1.441879115187,   5.0978125     ],
                                  [  1.441879115187,   4.325637345561,   5.0978125     ],
                                  [  4.325637345561,   4.325637345561,   5.0978125     ],
                                  [  7.209395575935,   4.325637345561,   5.0978125     ],
                                  [  1.441879115187,   7.209395575935,   5.0978125     ],
                                  [  4.325637345561,   7.209395575935,   5.0978125     ],
                                  [  7.209395575935,   7.209395575935,   5.0978125     ],
                                  [  0.            ,   0.            ,   7.1369375     ],
                                  [  2.883758230374,   0.            ,   7.1369375     ],
                                  [  5.767516460748,   0.            ,   7.1369375     ],
                                  [  0.            ,   2.883758230374,   7.1369375     ],
                                  [  2.883758230374,   2.883758230374,   7.1369375     ],
                                  [  5.767516460748,   2.883758230374,   7.1369375     ],
                                  [  0.            ,   5.767516460748,   7.1369375     ],
                                  [  2.883758230374,   5.767516460748,   7.1369375     ],
                                  [  5.767516460748,   5.767516460748,   7.1369375     ],
                                  [  1.441879115187,   1.441879115187,   9.1760625     ],
                                  [  4.325637345561,   1.441879115187,   9.1760625     ],
                                  [  7.209395575935,   1.441879115187,   9.1760625     ],
                                  [  1.441879115187,   4.325637345561,   9.1760625     ],
                                  [  4.325637345561,   4.325637345561,   9.1760625     ],
                                  [  7.209395575935,   4.325637345561,   9.1760625     ],
                                  [  1.441879115187,   7.209395575935,   9.1760625     ],
                                  [  4.325637345561,   7.209395575935,   9.1760625     ],
                                  [  7.209395575935,   7.209395575935,   9.1760625     ],
                                  [  0.            ,   0.            ,  11.2151875     ],
                                  [  2.883758230374,   0.            ,  11.2151875     ],
                                  [  5.767516460748,   0.            ,  11.2151875     ],
                                  [  0.            ,   2.883758230374,  11.2151875     ],
                                  [  2.883758230374,   2.883758230374,  11.2151875     ],
                                  [  5.767516460748,   2.883758230374,  11.2151875     ],
                                  [  0.            ,   5.767516460748,  11.2151875     ],
                                  [  2.883758230374,   5.767516460748,  11.2151875     ],
                                  [  5.767516460748,   5.767516460748,  11.2151875     ],
                                  [  1.441879115187,   1.441879115187,  13.2543125     ],
                                  [  4.325637345561,   1.441879115187,  13.2543125     ],
                                  [  7.209395575935,   1.441879115187,  13.2543125     ],
                                  [  1.441879115187,   4.325637345561,  13.2543125     ],
                                  [  4.325637345561,   4.325637345561,  13.2543125     ],
                                  [  7.209395575935,   4.325637345561,  13.2543125     ],
                                  [  1.441879115187,   7.209395575935,  13.2543125     ],
                                  [  4.325637345561,   7.209395575935,  13.2543125     ],
                                  [  7.209395575935,   7.209395575935,  13.2543125     ],
                                  [  0.            ,   0.            ,  15.2934375     ],
                                  [  2.883758230374,   0.            ,  15.2934375     ],
                                  [  5.767516460748,   0.            ,  15.2934375     ],
                                  [  0.            ,   2.883758230374,  15.2934375     ],
                                  [  2.883758230374,   2.883758230374,  15.2934375     ],
                                  [  5.767516460748,   2.883758230374,  15.2934375     ],
                                  [  0.            ,   5.767516460748,  15.2934375     ],
                                  [  2.883758230374,   5.767516460748,  15.2934375     ],
                                  [  5.767516460748,   5.767516460748,  15.2934375     ],
                                  [  1.441879115187,   1.441879115187,  17.3325625     ],
                                  [  4.325637345561,   1.441879115187,  17.3325625     ],
                                  [  7.209395575935,   1.441879115187,  17.3325625     ],
                                  [  1.441879115187,   4.325637345561,  17.3325625     ],
                                  [  4.325637345561,   4.325637345561,  17.3325625     ],
                                  [  7.209395575935,   4.325637345561,  17.3325625     ],
                                  [  1.441879115187,   7.209395575935,  17.3325625     ],
                                  [  4.325637345561,   7.209395575935,  17.3325625     ],
                                  [  7.209395575935,   7.209395575935,  17.3325625     ],
                                  [  0.            ,   0.            ,  19.3716875     ],
                                  [  2.883758230374,   0.            ,  19.3716875     ],
                                  [  5.767516460748,   0.            ,  19.3716875     ],
                                  [  0.            ,   2.883758230374,  19.3716875     ],
                                  [  2.883758230374,   2.883758230374,  19.3716875     ],
                                  [  5.767516460748,   2.883758230374,  19.3716875     ],
                                  [  0.            ,   5.767516460748,  19.3716875     ],
                                  [  2.883758230374,   5.767516460748,  19.3716875     ],
                                  [  5.767516460748,   5.767516460748,  19.3716875     ]]*Angstrom

    # Set up configuration
    central_region = BulkConfiguration(
        bravais_lattice=central_region_lattice,
        elements=central_region_elements,
        cartesian_coordinates=central_region_coordinates
        )

    device_configuration = DeviceConfiguration(
        central_region,
        [left_electrode, right_electrode]
        )

    # -------------------------------------------------------------
    # Calculator
    # -------------------------------------------------------------

    potentialSet = EAM_Au_Sheng_2011()
    calculator = TremoloXCalculator(parameters=potentialSet)
    calculator.setVerletListsDelta(0.25*Angstrom)

    device_configuration.setCalculator(calculator)
    nlprint(device_configuration)
    device_configuration.update()
    nlsave('Au100_L3_T300.nc', device_configuration)

    # -------------------------------------------------------------
    # Molecular Dynamics
    # -------------------------------------------------------------

    initial_velocity = MaxwellBoltzmannDistribution(
        temperature=300.0*Kelvin,
        remove_center_of_mass_momentum=True
    )

    method = Langevin(
        time_step=1*femtoSecond,
        reservoir_temperature=300*Kelvin,
        friction=0.01*femtoSecond**-1,
        initial_velocity=initial_velocity,
        heating_rate=0*Kelvin/picoSecond,
    )

    md_trajectory = MolecularDynamics(
        device_configuration,
        constraints=[],
        trajectory_filename='trajectory.nc',
        steps=5000,
        log_interval=100,
        method=method
    )

    device_configuration = md_trajectory.lastImage()
    nlsave('Au100_L3_T300.nc', md_trajectory)

    # -------------------------------------------------------------
    # Calculator
    # -------------------------------------------------------------
    #----------------------------------------
    # Basis Set
    #----------------------------------------
    basis_set = HotbitDirectory("hotbit/standard/")

    #----------------------------------------
    # Pair Potentials
    #----------------------------------------
    pair_potentials = HotbitDirectory("hotbit/standard/")

    #----------------------------------------
    # Numerical Accuracy Settings
    #----------------------------------------
    left_electrode_k_point_sampling = MonkhorstPackGrid(
        na=2,
        nb=2,
        nc=42,
        )
    left_electrode_numerical_accuracy_parameters = NumericalAccuracyParameters(
        k_point_sampling=left_electrode_k_point_sampling,
        )

    right_electrode_k_point_sampling = MonkhorstPackGrid(
        na=2,
        nb=2,
        nc=42,
        )
    right_electrode_numerical_accuracy_parameters = NumericalAccuracyParameters(
        k_point_sampling=right_electrode_k_point_sampling,
        )

    device_k_point_sampling = MonkhorstPackGrid(
        na=2,
        nb=2,
        nc=42,
        )
    device_numerical_accuracy_parameters = NumericalAccuracyParameters(
        k_point_sampling=device_k_point_sampling,
        )

    #----------------------------------------
    # Iteration Control Settings
    #----------------------------------------
    left_electrode_iteration_control_parameters = IterationControlParameters()

    right_electrode_iteration_control_parameters = IterationControlParameters()

    device_iteration_control_parameters = IterationControlParameters()

    #----------------------------------------
    # Poisson Solver Settings
    #----------------------------------------
    left_electrode_poisson_solver = FastFourier2DSolver(
        boundary_conditions=[[PeriodicBoundaryCondition(),PeriodicBoundaryCondition()],
                             [PeriodicBoundaryCondition(),PeriodicBoundaryCondition()],
                             [PeriodicBoundaryCondition(),PeriodicBoundaryCondition()]]
        )

    right_electrode_poisson_solver = FastFourier2DSolver(
        boundary_conditions=[[PeriodicBoundaryCondition(),PeriodicBoundaryCondition()],
                             [PeriodicBoundaryCondition(),PeriodicBoundaryCondition()],
                             [PeriodicBoundaryCondition(),PeriodicBoundaryCondition()]]
        )

    #----------------------------------------
    # Electrode Calculators
    #----------------------------------------
    left_electrode_calculator = SlaterKosterCalculator(
        basis_set=basis_set,
        pair_potentials=pair_potentials,
        numerical_accuracy_parameters=left_electrode_numerical_accuracy_parameters,
        iteration_control_parameters=left_electrode_iteration_control_parameters,
        poisson_solver=left_electrode_poisson_solver,
        )

    right_electrode_calculator = SlaterKosterCalculator(
        basis_set=basis_set,
        pair_potentials=pair_potentials,
        numerical_accuracy_parameters=right_electrode_numerical_accuracy_parameters,
        iteration_control_parameters=right_electrode_iteration_control_parameters,
        poisson_solver=right_electrode_poisson_solver,
        )

    #----------------------------------------
    # Device Calculator
    #----------------------------------------
    calculator = DeviceSlaterKosterCalculator(
        basis_set=basis_set,
        pair_potentials=pair_potentials,
        numerical_accuracy_parameters=device_numerical_accuracy_parameters,
        iteration_control_parameters=device_iteration_control_parameters,
        electrode_calculators=
            [left_electrode_calculator, right_electrode_calculator],
        )

    device_configuration.setCalculator(calculator)
    nlprint(device_configuration)
    device_configuration.update()
    nlsave('Au100_L3_T300.nc', device_configuration)

    # -------------------------------------------------------------
    # Transmission Spectrum
    # -------------------------------------------------------------
    kpoint_grid = MonkhorstPackGrid(
        na=10,
        nb=10,
        )

    transmission_spectrum = TransmissionSpectrum(
        configuration=device_configuration,
        energies=numpy.linspace(-0.2,0.2,51)*eV,
        kpoints=kpoint_grid,
        energy_zero_parameter=AverageFermiLevel,
        infinitesimal=1e-06*eV,
        self_energy_calculator=KrylovSelfEnergy(),
        )
    nlsave('Au100_L3_T300.nc', transmission_spectrum)
    nlprint(transmission_spectrum)
