# Set minimal log verbosity
setVerbosity(MinimalLog)

# %% Device: BulkConfiguration_1

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

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

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

# Define coordinates
left_electrode_coordinates = [[ 0.716625      ,  0.716625      ,  0.700511523087],
                              [ 2.149875      ,  2.149875      ,  2.133761523085],
                              [ 0.716625      ,  0.716625      ,  3.567011523083],
                              [ 2.149875      ,  2.149875      ,  5.000261523082],
                              [ 0.716625      ,  0.716625      ,  6.43351152308 ],
                              [ 2.149875      ,  2.149875      ,  7.866761523078]]*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 = [2.8665, 0.0, 0.0]*Angstrom
vector_b = [0.0, 2.8665, 0.0]*Angstrom
vector_c = [0.0, 0.0, 8.567259344664997]*Angstrom
right_electrode_lattice = UnitCell(vector_a, vector_b, vector_c)

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

# Define coordinates
right_electrode_coordinates = [[ 2.149875      ,  2.149875      ,  0.700504672337],
                               [ 0.716625      ,  0.716625      ,  2.133754672335],
                               [ 2.149875      ,  2.149875      ,  3.567004672333],
                               [ 0.716625      ,  0.716625      ,  5.000254672332],
                               [ 2.149875      ,  2.149875      ,  6.43350467233 ],
                               [ 0.716625      ,  0.716625      ,  7.866754672328]]*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 = [2.8665, 0.0, 0.0]*Angstrom
vector_b = [0.0, 2.8665, 0.0]*Angstrom
vector_c = [0.0, 0.0, 47.06911399600941]*Angstrom
central_region_lattice = UnitCell(vector_a, vector_b, vector_c)

# Define elements
central_region_elements = [Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron,
                           Magnesium, Oxygen, Magnesium, Oxygen, Magnesium, Oxygen, Magnesium,
                           Oxygen, Oxygen, Magnesium, Oxygen, Magnesium, Oxygen, Magnesium,
                           Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron, Iron]

# Define coordinates
central_region_coordinates = [[  0.716625      ,   0.716625      ,   0.700511523087],
                              [  2.149875      ,   2.149875      ,   2.133761523085],
                              [  0.716625      ,   0.716625      ,   3.567011523083],
                              [  2.149875      ,   2.149875      ,   5.000261523082],
                              [  0.716625      ,   0.716625      ,   6.43351152308 ],
                              [  2.149875      ,   2.149875      ,   7.866761523078],
                              [  0.716625      ,   0.716625      ,   9.267784569251],
                              [  2.149875      ,   2.149875      ,  10.677503636183],
                              [  0.716625      ,   0.716625      ,  12.081479072703],
                              [  2.149875      ,   2.149875      ,  13.499957315388],
                              [  0.716625      ,   0.716625      ,  14.844300102445],
                              [  2.149875000001,   2.149875      ,  16.942579441772],
                              [  0.716624999999,   0.716625      ,  17.017554267971],
                              [  0.716625      ,   0.716625      ,  19.168635959833],
                              [  2.149875      ,   2.149875      ,  19.18306254133 ],
                              [  2.149874999999,   2.149875      ,  21.348621287264],
                              [  0.716625000001,   0.716625      ,  21.358563567876],
                              [  0.716625      ,   0.716625      ,  23.534124615213],
                              [  2.149874999999,   2.149875      ,  23.534143527941],
                              [  0.716625000001,   0.716625      ,  25.709966074125],
                              [  2.149874999999,   2.149875      ,  25.719534508285],
                              [  2.149875      ,   2.149875      ,  27.885652490889],
                              [  0.716625      ,   0.716625      ,  27.899618113528],
                              [  0.716625      ,   0.716625000001,  30.050742634662],
                              [  2.149875      ,   2.149874999999,  30.125179797083],
                              [  0.716625      ,   0.716625      ,  32.224002118919],
                              [  2.149875      ,   2.149875      ,  33.568882073837],
                              [  0.716625      ,   0.716625      ,  34.987730615659],
                              [  2.149875      ,   2.149875      ,  36.391795273303],
                              [  0.716625      ,   0.716625      ,  37.801349979008],
                              [  2.149875      ,   2.149875      ,  39.202359323681],
                              [  0.716625      ,   0.716625      ,  40.63560932368 ],
                              [  2.149875      ,   2.149875      ,  42.068859323678],
                              [  0.716625      ,   0.716625      ,  43.502109323676],
                              [  2.149875      ,   2.149875      ,  44.935359323674],
                              [  0.716625      ,   0.716625      ,  46.368609323673]]*Angstrom

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

device_bulkconfiguration_1 = DeviceConfiguration(
    central_region,
    [left_electrode, right_electrode],
    equivalent_electrode_lengths=[8.567273046165, 8.567259344665]*Angstrom,
    transverse_electrode_repetitions=[[1, 1], [1, 1]],
    )

# Add tags
device_bulkconfiguration_1.addTags('layer 0',   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
device_bulkconfiguration_1.addTags('layer 1',   [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
                                                 24])
device_bulkconfiguration_1.addTags('layer 2',   [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35])
device_bulkconfiguration_1.addTags('left fix',  [0, 1, 2, 3, 4, 5])
device_bulkconfiguration_1.addTags('right fix', [30, 31, 32, 33, 34, 35])


# %% Set DeviceLCAOCalculator

# %% DeviceLCAOCalculator

#----------------------------------------
# Exchange-Correlation
#----------------------------------------
exchange_correlation = NCGGA.PBE

k_point_sampling = KpointDensity(
    density_a=7.0*Angstrom,
    density_b=7.0*Angstrom,
    density_c=150.0*Angstrom
)

numerical_accuracy_parameters = NumericalAccuracyParameters(
    k_point_sampling=k_point_sampling
)

iteration_control_parameters = IterationControlParameters(
    tolerance=1e-05
)


initial_density_type = EquivalentBulk()

device_algorithm_parameters = DeviceAlgorithmParameters(
    initial_density_type=initial_density_type
)

calculator = DeviceLCAOCalculator(
    exchange_correlation=exchange_correlation,
    numerical_accuracy_parameters=numerical_accuracy_parameters,
    iteration_control_parameters=iteration_control_parameters,
    device_algorithm_parameters=device_algorithm_parameters,
    checkpoint_handler=NoCheckpointHandler
)


# %% Set Calculator

device_bulkconfiguration_1.setCalculator(calculator)

nlsave('Fe-MgO7L-Fe-STT-Medium.hdf5', device_bulkconfiguration_1)


# %% FiniteBiasSpinTransferTorque

kpoints = KpointDensity(
    density_a=17.0*Angstrom,
    density_b=17.0*Angstrom,
    density_c=0.0*Angstrom
)

finite_bias_spin_transfer_torque = FiniteBiasSpinTransferTorque(
    configuration=device_bulkconfiguration_1,
    filename='Fe-MgO7L-Fe-STT-Medium.hdf5',
    object_id='stt',
    atom_indices=['layer 2'],
    bias_voltages=numpy.linspace(-1.0, 1.0, 21)*Volt,
    kpoints=kpoints,
    potential_drop_region_tags=['layer 1'],
    number_of_processes_per_task=ProcessesPerNode,
    log_filename_prefix='finite_bias_stt_'
)

finite_bias_spin_transfer_torque.update()
