# -*- coding: utf-8 -*-
setVerbosity(MinimalLog)

# -------------------------------------------------------------
# Bulk Configuration
# -------------------------------------------------------------

# Set up lattice
lattice = Hexagonal(4.268144746973739*Angstrom, 17.279487156342892*Angstrom)

# Define elements
elements = [Germanium, Germanium, Antimony, Antimony, Tellurium, Tellurium,
            Tellurium, Tellurium, Tellurium]

# Define coordinates
fractional_coordinates = [[ 0.583333333333,  0.916666666667,  0.950882364059],
                          [ 0.916666666667,  0.583333333333,  0.152012635941],
                          [ 0.25          ,  0.25          ,  0.73576708634 ],
                          [ 0.245034529464,  0.254965470536,  0.367127896968],
                          [ 0.25          ,  0.25          ,  0.0514475     ],
                          [ 0.916666666667,  0.583333333333,  0.852146243706],
                          [ 0.583333333333,  0.916666666667,  0.250748756294],
                          [ 0.583333333333,  0.916666666667,  0.637741982838],
                          [ 0.911701196131,  0.588298803869,  0.46515300047 ]]

# Define ghost atoms
ghost_atoms = [3]

# Set up configuration
bulk_configuration = BulkConfiguration(
    bravais_lattice=lattice,
    elements=elements,
    fractional_coordinates=fractional_coordinates,
    ghost_atoms=ghost_atoms
    )

# -------------------------------------------------------------
# Calculator
# -------------------------------------------------------------
k_point_sampling = KpointDensity(
    density_a=4.0*Angstrom,
    )
numerical_accuracy_parameters = NumericalAccuracyParameters(
    density_mesh_cutoff=120.0*Hartree,
    k_point_sampling=k_point_sampling,
    )

calculator = LCAOCalculator(
    numerical_accuracy_parameters=numerical_accuracy_parameters,
    )

bulk_configuration.setCalculator(calculator)
nlprint(bulk_configuration)
bulk_configuration.update()
nlsave('PBE-ghost.hdf5', bulk_configuration)

# -------------------------------------------------------------
# Optimize Geometry
# -------------------------------------------------------------
bulk_configuration = OptimizeGeometry(
    bulk_configuration,
    max_forces=0.05*eV/Ang,
    max_steps=200,
    max_step_length=0.2*Ang,
    trajectory_filename='PBE-ghost_trajectory.hdf5',
    trajectory_interval=1,
    restart_strategy=RestartFromTrajectory(),
    disable_stress=True,
    optimizer_method=LBFGS(),
    enable_optimization_stop_file=True,
)
nlsave('PBE-ghost.hdf5', bulk_configuration)
nlprint(bulk_configuration)

# -------------------------------------------------------------
# Total Energy
# -------------------------------------------------------------
total_energy = TotalEnergy(bulk_configuration)
nlsave('PBE-ghost.hdf5', total_energy)
nlprint(total_energy)

# -------------------------------------------------------------
# Bandstructure
# -------------------------------------------------------------
bandstructure = Bandstructure(
    configuration=bulk_configuration,
    route=['G', 'M', 'L', 'A', 'G', 'K', 'H', 'A'],
    points_per_segment=50,
    bands_above_fermi_level=All,
    method=Full,
    )
nlsave('PBE-ghost.hdf5', bandstructure)

# -------------------------------------------------------------
# Projected Density Of States
# -------------------------------------------------------------
kpoint_grid = KpointDensity(
    density_a=7.0*Angstrom,
    )

projected_density_of_states = ProjectedDensityOfStates(
    configuration=bulk_configuration,
    kpoints=kpoint_grid,
    projections=ProjectOnSites,
    energies=numpy.linspace(-8, 8, 601)*eV,
    energy_zero_parameter=FermiLevel,
    bands_above_fermi_level=All,
    spectrum_method=TetrahedronMethod,
)
nlsave('PBE-ghost.hdf5', projected_density_of_states)
