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

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

# Set up lattice
vector_a = [5.13498, 0.0, 0.0]*Angstrom
vector_b = [0.0, -3.45897, 0.0]*Angstrom
vector_c = [0.0, 0.0, 9.23927881]*Angstrom
left_electrode_lattice = UnitCell(vector_a, vector_b, vector_c)

# Define elements
left_electrode_elements = [Oxygen, Oxygen, Carbon, Oxygen, Lithium, Lithium, Oxygen, Oxygen,
                           Carbon, Oxygen, Lithium, Lithium]

# Define coordinates
left_electrode_coordinates = [[ 1.95449772104 ,  0.            ,  0.2177065811  ],
                              [ 3.915580315227,  0.            ,  1.35769133165 ],
                              [ 2.600173419298,  0.            ,  1.36526111845 ],
                              [ 1.964433634369,  0.            ,  2.523699288   ],
                              [ 0.02237185304 ,  0.            ,  2.9007034941  ],
                              [ 2.567490552632, -1.729486487679,  4.4019328222  ],
                              [ 4.521988273666, -1.729486487679,  4.8373459844  ],
                              [ 1.34808976259 , -1.729486487679,  5.97733073495 ],
                              [ 0.032682866661, -1.729486487679,  5.98490052175 ],
                              [ 4.531924186996, -1.729486487679,  7.1433386913  ],
                              [ 2.589862405672, -1.729486487679,  7.5203428974  ],
                              [-0.00000000001 ,  0.            ,  9.0215722255  ]]*Angstrom

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

# Add tags
left_electrode.addTags('Left Interface')
left_electrode.addTags('li2co3_electrode')

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

# Set up lattice
vector_a = [5.13498, 0.0, 0.0]*Angstrom
vector_b = [0.0, -3.45897, 0.0]*Angstrom
vector_c = [0.0, 0.0, 7.70665406]*Angstrom
right_electrode_lattice = UnitCell(vector_a, vector_b, vector_c)

# Define elements
right_electrode_elements = [Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium,
                            Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium]

# Define coordinates
right_electrode_coordinates = [[ 3.354031312343,  0.00000080576 ,  0.569549537625],
                               [ 0.786540759712, -1.729485681922,  0.569549537625],
                               [ 1.642363496849,  0.00000080576 ,  1.357168037875],
                               [ 4.209854049486, -1.729485681922,  1.357168037875],
                               [ 3.354031312343,  0.00000080576 ,  2.144786538175],
                               [ 0.786540759712, -1.729485681922,  2.144786538175],
                               [-0.06929672078 ,  0.000000805757,  3.283831552725],
                               [ 2.498193831857, -1.729485681922,  3.283831552725],
                               [ 3.354031312343,  0.00000080576 ,  4.422876567275],
                               [ 0.786540759712, -1.729485681922,  4.422876567275],
                               [ 1.642363496849,  0.00000080576 ,  5.210495067525],
                               [ 4.209854049486, -1.729485681922,  5.210495067525],
                               [ 3.354031312343,  0.00000080576 ,  5.998113567825],
                               [ 0.786540759712, -1.729485681922,  5.998113567825],
                               [-0.06929672078 ,  0.000000805757,  7.137158582375],
                               [ 2.498193831857, -1.729485681922,  7.137158582375]]*Angstrom

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

# Add tags
right_electrode.addTags('Right Interface')
right_electrode.addTags('li2o2_electrode')

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

# Set up lattice
vector_a = [5.13498, 0.0, 0.0]*Angstrom
vector_b = [0.0, -3.45897, 0.0]*Angstrom
vector_c = [0.0, 0.0, 36.9510044716]*Angstrom
central_region_lattice = UnitCell(vector_a, vector_b, vector_c)

# Define elements
central_region_elements = [Oxygen, Oxygen, Carbon, Oxygen, Lithium, Lithium, Oxygen, Oxygen,
                           Carbon, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Carbon, Oxygen,
                           Lithium, Lithium, Oxygen, Carbon, Oxygen, Oxygen, Lithium, Lithium,
                           Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium,
                           Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium,
                           Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium,
                           Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium,
                           Oxygen, Oxygen, Lithium, Lithium, Oxygen, Oxygen, Lithium, Lithium]

# Define coordinates
central_region_coordinates = [[  1.95449772104 ,   0.            ,   0.2177065811  ],
                              [  3.915580315227,   0.            ,   1.35769133165 ],
                              [  2.600173419298,   0.            ,   1.36526111845 ],
                              [  1.964433634369,   0.            ,   2.523699288   ],
                              [  0.02237185304 ,   0.            ,   2.9007034941  ],
                              [  2.567490552632,  -1.729486487679,   4.4019328222  ],
                              [  4.521988273666,  -1.729486487679,   4.8373459844  ],
                              [  1.34808976259 ,  -1.729486487679,   5.97733073495 ],
                              [  0.032682866661,  -1.729486487679,   5.98490052175 ],
                              [  4.531924186996,  -1.729486487679,   7.1433386913  ],
                              [  2.589862405672,  -1.729486487679,   7.5203428974  ],
                              [ -0.00000000001 ,   0.            ,   9.0215722255  ],
                              [  1.954497721029,   0.            ,   9.4569853877  ],
                              [  3.945314545152,  -0.000000113638,  10.52710295575 ],
                              [  2.640452127956,  -0.000000093129,  10.5606693969  ],
                              [  2.039468692835,  -0.000000167525,  11.74539501775 ],
                              [  0.003190182949,  -0.000000167214,  12.1112079491  ],
                              [  2.542133175927,  -1.72948515042 ,  13.0038880872  ],
                              [  4.663415583007,  -1.729485096156,  13.37961883405 ],
                              [  0.070262755592,  -1.729485180883,  14.5760847716  ],
                              [  1.362853481672,  -1.729485081943,  14.719331755   ],
                              [  4.444143531112,  -1.729485349781,  15.6544338432  ],
                              [  2.546018422478,  -1.729485368048,  16.40394858635 ],
                              [ -0.129192281474,  -0.000000581259,  16.8792506737  ],
                              [  1.606971761567,  -0.000000122306,  17.846172999   ],
                              [  4.174697325883,  -1.729485031774,  18.156685102   ],
                              [  0.876797539826,  -1.729485158787,  18.7354601751  ],
                              [  3.358267279039,  -0.000000024427,  18.79070052455 ],
                              [  1.589137522931,  -0.000000119781,  19.4293944483  ],
                              [  4.200439413184,  -1.729485000014,  19.7234065001  ],
                              [ -0.049898134669,  -0.00000010307 ,  20.7109546466  ],
                              [  2.602557257087,  -1.72948498651 ,  20.88016266925 ],
                              [  0.965044406537,  -1.72948508818 ,  22.05284837795 ],
                              [  3.516669946255,  -0.0000001951  ,  22.05690063695 ],
                              [  4.319439809132,  -1.7294851772  ,  22.8056975305  ],
                              [  1.718116545687,  -0.000000100026,  22.8104581491  ],
                              [  0.946891217389,  -1.729485120348,  23.6274824219  ],
                              [  3.504359114494,  -0.000000160216,  23.6306694038  ],
                              [  2.58671840814 ,  -1.729485146252,  24.7605492418  ],
                              [  0.010617943828,  -0.000000097166,  24.77348971925 ],
                              [  1.642356029879,   0.00000080576 ,  25.9650451078  ],
                              [  4.209846582516,  -1.729485681922,  25.9650451078  ],
                              [  3.35402407804 ,   0.00000080576 ,  26.74829119525 ],
                              [  0.786533525403,  -1.729485681922,  26.74829119525 ],
                              [  1.642356029879,   0.00000080576 ,  27.5315372826  ],
                              [  4.209846582516,  -1.729485681922,  27.5315372826  ],
                              [ -0.06929672078 ,   0.000000805757,  28.6748549347  ],
                              [  2.498193831857,  -1.729485681922,  28.6748549347  ],
                              [  3.354031312343,   0.00000080576 ,  29.81389994925 ],
                              [  0.786540759712,  -1.729485681922,  29.81389994925 ],
                              [  1.642363496849,   0.00000080576 ,  30.6015184495  ],
                              [  4.209854049486,  -1.729485681922,  30.6015184495  ],
                              [  3.354031312343,   0.00000080576 ,  31.3891369498  ],
                              [  0.786540759712,  -1.729485681922,  31.3891369498  ],
                              [ -0.06929672078 ,   0.000000805757,  32.52818196435 ],
                              [  2.498193831857,  -1.729485681922,  32.52818196435 ],
                              [  3.354031312343,   0.00000080576 ,  33.6672269789  ],
                              [  0.786540759712,  -1.729485681922,  33.6672269789  ],
                              [  1.642363496849,   0.00000080576 ,  34.45484547915 ],
                              [  4.209854049486,  -1.729485681922,  34.45484547915 ],
                              [  3.354031312343,   0.00000080576 ,  35.24246397945 ],
                              [  0.786540759712,  -1.729485681922,  35.24246397945 ],
                              [ -0.06929672078 ,   0.000000805757,  36.381508994   ],
                              [  2.498193831857,  -1.729485681922,  36.381508994   ]]*Angstrom

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

# Add tags
central_region.addTags('Left Interface',   [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
                                            13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
central_region.addTags('Right Interface',  [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
                                            37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
                                            50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
                                            63])
central_region.addTags('li2co3_electrode', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
central_region.addTags('li2o2_electrode',  [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
                                            53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63])

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

# -------------------------------------------------------------
# Calculator
# -------------------------------------------------------------
#----------------------------------------
# Basis Set
#----------------------------------------
basis_set = [
    GGABasis.Lithium_DoubleZetaPolarized,
    GGABasis.Carbon_DoubleZetaPolarized,
    GGABasis.Oxygen_DoubleZetaPolarized,
    ]

#----------------------------------------
# Exchange-Correlation
#----------------------------------------
exchange_correlation = GGA.RPBE

#----------------------------------------
# Numerical Accuracy Settings
#----------------------------------------
left_electrode_numerical_accuracy_parameters = NumericalAccuracyParameters(
    k_point_sampling=(7, 15, 200),
    )

right_electrode_numerical_accuracy_parameters = NumericalAccuracyParameters(
    k_point_sampling=(7, 15, 200),
    )

device_numerical_accuracy_parameters = NumericalAccuracyParameters(
    k_point_sampling=(7, 15, 200),
    )

#----------------------------------------
# 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 = LCAOCalculator(
    basis_set=basis_set,
    exchange_correlation=exchange_correlation,
    numerical_accuracy_parameters=left_electrode_numerical_accuracy_parameters,
    poisson_solver=left_electrode_poisson_solver,
    )

right_electrode_calculator = LCAOCalculator(
    basis_set=basis_set,
    exchange_correlation=exchange_correlation,
    numerical_accuracy_parameters=right_electrode_numerical_accuracy_parameters,
    poisson_solver=right_electrode_poisson_solver,
    )

#----------------------------------------
# Device Calculator
#----------------------------------------
calculator = DeviceLCAOCalculator(
    basis_set=basis_set,
    exchange_correlation=exchange_correlation,
    numerical_accuracy_parameters=device_numerical_accuracy_parameters,
    electrode_calculators=
        [left_electrode_calculator, right_electrode_calculator],
    )

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

# -------------------------------------------------------------
# Transmission Spectrum
# -------------------------------------------------------------
transmission_spectrum = TransmissionSpectrum(
    configuration=device_configuration,
    energies=numpy.linspace(-3,3,201)*eV,
    kpoints=MonkhorstPackGrid(13,21),
    energy_zero_parameter=AverageFermiLevel,
    infinitesimal=1e-06*eV,
    self_energy_calculator=RecursionSelfEnergy(),
    )
nlsave('device_interface.nc', transmission_spectrum)
nlprint(transmission_spectrum)

# -------------------------------------------------------------
# Projected Local Density Of States
# -------------------------------------------------------------
projected_local_density_of_states = ProjectedLocalDensityOfStates(
    configuration=device_configuration,
    method=DeviceDensityOfStates,
    energies=numpy.linspace(-4, 4, 301)*eV,
    kpoints=MonkhorstPackGrid(13, 21),
    contributions=All,
    self_energy_calculator=RecursionSelfEnergy(),
    energy_zero_parameter=AverageFermiLevel,
    infinitesimal=1e-06*eV,
    )
nlsave('device_interface.nc', projected_local_density_of_states)

