# -*- coding: utf-8 -*-
# -------------------------------------------------------------
# Bulk Configuration
# -------------------------------------------------------------

# Set up lattice
lattice = Rhombohedral(5.05739*Angstrom, 58.0551*Degrees)

# Define elements
elements = [Boron, Boron, Boron, Boron, Boron, Boron, Boron, Boron, Boron,
            Boron, Boron, Boron]

# Define coordinates
fractional_coordinates = [[ 0.6573,  0.0104,  0.0104],
                          [ 0.0104,  0.0104,  0.6573],
                          [ 0.3427,  0.9896,  0.9896],
                          [ 0.0104,  0.6573,  0.0104],
                          [ 0.9896,  0.9896,  0.3427],
                          [ 0.9896,  0.3427,  0.9896],
                          [ 0.6323,  0.2206,  0.2206],
                          [ 0.2206,  0.2206,  0.6323],
                          [ 0.3677,  0.7794,  0.7794],
                          [ 0.2206,  0.6323,  0.2206],
                          [ 0.7794,  0.7794,  0.3677],
                          [ 0.7794,  0.3677,  0.7794]]

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

# -------------------------------------------------------------
# Calculator
# -------------------------------------------------------------
k_point_sampling = MonkhorstPackGrid(
    na=7,
    nb=7,
    nc=7,
    )
numerical_accuracy_parameters = NumericalAccuracyParameters(
    k_point_sampling=k_point_sampling,
    density_mesh_cutoff=55.0*Hartree,
    )

calculator = LCAOCalculator(
    numerical_accuracy_parameters=numerical_accuracy_parameters,
    )

bulk_configuration.setCalculator(calculator)
nlprint(bulk_configuration)
bulk_configuration.update()
nlsave('bulk-boron.hdf5', bulk_configuration)

# -------------------------------------------------------------
# Optimize Geometry
# -------------------------------------------------------------
fix_atom_indices_0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

constraints = [FixAtomConstraints(fix_atom_indices_0)]

bulk_configuration = OptimizeGeometry(
    bulk_configuration,
    max_forces=0.05*eV/Ang,
    max_stress=0.1*GPa,
    max_steps=200,
    max_step_length=0.2*Ang,
    constraints=constraints,
    trajectory_filename=None,
    optimizer_method=LBFGS(),
    constrain_bravais_lattice=True,
)
nlsave('bulk-boron.hdf5', bulk_configuration)
nlprint(bulk_configuration)

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